【C语言】实现对一个8 bit数据(unsigned char类型)的指定位(例如第n位)的置0或者置1操作,并保持其他位不变。

  1. 功能:实现对一个8 bit数据(unsigned char类型)的指定位(例如第n位)的置0或者置1操作,并保持其他位不变。
  2. 函数原型:void bit_set(unsigned char *date, unsigned char position, int flag)
  3. date是一个8bitunsigned char类型的数据,position是指定位,flag是指定位置0还是置1.
  4. 还有几点是需要注意的,就是指针传参,一定要断言。
  5. 也要判断输入的值是否有误。
  6. 函数的具体思路就是先找到指定位,然后把指定位拿出来与flag进行比较,如果一致,则不改动,否则就改变。
  7. 最后输出从低地址向高地址位输出。
#include<stdio.h>
#include<assert.h>

void bit_set(unsigned char *date, unsigned char position, int flag)
{
    assert(date);
    if (position > 8 || position < 1 || (flag != 0 && flag != 1))
    {
        printf("输入有误!\n");
    }
    if (flag != (*date >> (position - 1)) & 1)
    {
        *date ^= 1 << (position - 1);
    }
    for (int i = 7; i >= 0; i--)
    {
        printf("%d", (*date >> i) & 1);
    }
    printf("\n");
}

int main()
{
    unsigned char date ;
    int flag ;
    unsigned char position ;
    printf("请输入数据和指定位和指定位置0还是1:>");
    scanf_s("%d %d %d", &date, &position, &flag);
    bit_set(&date, position, flag);
    system("pause");
    return 0;
}

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
// 2016/2/21 10:32 准备再次解答这个问题了。小学的时候使用人工枚举方法,这个我不擅长!现在决定用程序来解决了! // 2016/2/22 16:26 代码优化整理完成!!!! // 给你一个高难度的题目。 // 有一个14数。 // 由 2个1 2个2 2个3 2个4 2个5 2个6 2个7组成 // 其中: // 2个1中有1个数字。(衍生题目) // 2个2中有2个数字 // 2个3中有3个数字 // 2个4中有4个数字 // 2个5中有5个数字。 // 2个6中有6个数字。 // 2个7中有7个数字 // // 请给出一个 这样的14数字(总数大于10个。给一个即可!) // 这是我小学6年级的时候的 奥数考题! #include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> int main(void) { unsigned char ucArray[15] = {0}; // int i = 0; // for(i=1; i<15; i++) // { // printf("*ucArray[%d]=%d\n", i, ucArray[i]); // } // 2016/2/21 11:10 第一数字完成 int bit01 = 0; for(bit01=1; bit01<=7; bit01++) { unsigned char ucArray01[15] = {0}; int temp01 = 0; for(temp01=0; temp01<15; temp01++) { ucArray01[temp01] = ucArray[temp01]; } ucArray01[1] = bit01; ucArray01[1+bit01+1] = bit01; // for(temp01=1; temp01<15; temp01++) // { // printf("%d=%d ", temp01, ucArray01[temp01]); // } // // printf("*1\n"); // 2016/2/21 11:33 第二数字完成 int bit02 = 0; for(bit02=1; bit02<=7; bit02++) { unsigned char ucArray02[15] = {0}; int temp02 = 0; for(temp02=0; temp02<15; temp02++) { ucArray02[temp02] = ucArray01[temp02]; } // 2016/2/21 11:17 需要判断第2数字 和 接着的数字必须没有被占用。还有就是不能重复。 if(0==ucArray02[2]) { for(temp02=1; temp02<15; temp02++) { if(ucArray02[temp02]>0) { // continue; if(ucArray02[temp02]==bit02) { temp02 = 99; break; } } } if(temp02>15) { continue; } if(0==ucArray02[2+bit02+1]) { ucArray02[2] = bit02; ucArray02[2+bit02+1] = bit02; } else { continue; } } // for(temp02=1; temp02<15; temp02++) // { // printf("%d=%d ", temp02, ucArray02[temp02]); // } // // printf("*2\n"); // 2016/2/21 20:18 第三数字完工 int bit03 = 0; for(bit03=1; bit03<=7; bit03++) { unsigned char ucArray03[15] = {0}; int temp03 = 0; for(temp03=0; temp03<15; temp03++) { ucArray03[temp03] = ucArray02[temp03]; } // 2016/2/21 11:17 需要判断第2数字 和 接着的数字必须没有被占用。还有就是不能重复 if(0==ucArray03[3]) { for(temp03=0; temp03<15; temp03++) { if(ucArray03[temp03]>0) { if(ucArray03[temp03]==bit03) { // continue; temp03 = 99; break; } } } if(temp03>15) { continue; } if(0==ucArray03[3+bit03+1]) { ucArray03[3] = bit03; ucArray03[3+bit03+1] = bit03; } else { continue; } } else { bit03 = 8; } // for(temp03=1; temp03<15; temp03++) // { // printf("%d=%d ", temp03, ucArray03[temp03]); // } // // printf("*3\n"); // 2016/2/21 20:18 第四数字(将所有的03替换为04,还有就是02替换为03) int bit04 = 0; for(bit04=1; bit04<=7; bit04++) { unsigned char ucArray04[15] = {0}; int temp04 = 0; for(temp04=0; temp04<15; temp04++) { ucArray04[temp04] = ucArray03[temp04]; } // 2016/2/21 11:17 需要判断第2数字 和 接着的数字必须没有被占用。还有就是不能重复 if(0==ucArray04[4]) { for(temp04=1; temp04<15; temp04++) { if(ucArray04[temp04]>0) { if(ucArray04[temp04]==bit04) { // continue; temp04 = 99; break; } } } if(temp04>15) { continue; } if(0==ucArray04[4+bit04+1]) { ucArray04[4] = bit04; ucArray04[4+bit04+1] = bit04; } else { continue; } } else { bit04 = 8; } // for(temp04=1; temp04<15; temp04++) // { // printf("%d=%d ", temp04, ucArray04[temp04]); // } // // printf("*4\n"); // 2016/2/21 20:30 第5数字 int bit05 = 0; for(bit05=1; bit05<=7; bit05++) { unsigned char ucArray05[15] = {0}; int temp05 = 0; for(temp05=0; temp05<15; temp05++) { ucArray05[temp05] = ucArray04[temp05]; } // 2016/2/21 11:17 需要判断第2数字 和 接着的数字必须没有被占用。还有就是不能重复 if(0==ucArray05[5]) { for(temp05=1; temp05<15; temp05++) { if(ucArray05[temp05]>0) { if(ucArray05[temp05]==bit05) { // continue; temp05 = 99; break; } } } if(temp05>15) { continue; } if(0==ucArray05[5+bit05+1]) { ucArray05[5] = bit05; ucArray05[5+bit05+1] = bit05; } else { continue; } } else { bit05 = 8; } // for(temp05=1; temp05<15; temp05++) // { // printf("%d=%d ", temp05, ucArray05[temp05]); // } // // printf("*5\n"); // 2016/2/21 20:38 第6数字 int bit06 = 0; for(bit06=1; bit06<=7; bit06++) { unsigned char ucArray06[15] = {0}; int temp06 = 0; for(temp06=0; temp06<15; temp06++) { ucArray06[temp06] = ucArray05[temp06]; } if(0==ucArray06[6]) { for(temp06=1; temp06<15; temp06++) { if(ucArray06[temp06]>0) { if(ucArray06[temp06]==bit06) { temp06 = 99; break; } } } // 2016/2/22 12:20 清除重复数字 if(temp06>15) { continue; } if(0==ucArray06[6+bit06+1]) { ucArray06[6] = bit06; ucArray06[6+bit06+1] = bit06; } else { continue; } } else { bit06 = 8; } // for(temp06=1; temp06<15; temp06++) // { // printf("%d=%d ", temp06, ucArray06[temp06]); // } // // printf("*6\n"); // 2016/2/21 21:52 第7数字 int bit07 = 0; for(bit07=1; bit07<=7; bit07++) { unsigned char ucArray07[15] = {0}; int temp07 = 0; for(temp07=0; temp07<15; temp07++) { ucArray07[temp07] = ucArray06[temp07]; } // 2016/2/22 12:24 使用新的逻辑 if(0==ucArray07[7]) { for(temp07=1; temp07<15; temp07++) { if(ucArray07[temp07]>0) { if(ucArray07[temp07]==bit07) { temp07 = 99; break; } } } // 2016/2/22 12:20 清除重复数字 if(temp07>15) { continue; } if((7+bit07+1)>14) { continue; } if(0==ucArray07[7+bit07+1]) { // 357436 25427161* 漏了这一句 ucArray07[7] = bit07; ucArray07[7+bit07+1] = bit07; } else { continue; } } else { bit07 = 8; } // for(temp07=1; temp07<15; temp07++) // { // printf("%d=%d ", temp07, ucArray07[temp07]); // } // // printf("*7\n"); // 2016/2/21 21:58 第8个数字 int bit08 = 0; for(bit08=1; bit08<=7; bit08++) { unsigned char ucArray08[15] = {0}; int temp08 = 0; for(temp08=0; temp08<15; temp08++) { ucArray08[temp08] = ucArray07[temp08]; } if(0==ucArray08[8]) { for(temp08=1; temp08<15; temp08++) { if(ucArray08[temp08]>0) { if(ucArray08[temp08]==bit08) { temp08 = 99; break; } } } if(temp08>15) { continue; } // 2016/2/22 11:42 数组越界检查(超过14了) if((8+bit08+1)>14) { continue; } if(0==ucArray08[8+bit08+1]) { ucArray08[8] = bit08; ucArray08[8+bit08+1] = bit08; } } else { bit08 = 8; } // for(temp08=1; temp08<15; temp08++) // { // printf("%d=%d ", temp08, ucArray08[temp08]); // } // // printf("*8\n"); // 2016/2/21 22:03 第9数字 填字的话只剩下这种情况(最后六:431614):xxxxxxxx431614 int bit09 = 0; // for(bit09=1; bit09<=7; bit09++) for(bit09=1; bit09<=4; bit09++) { unsigned char ucArray09[15] = {0}; int temp09 = 0; for(temp09=0; temp09<15; temp09++) { ucArray09[temp09] = ucArray08[temp09]; } if(0==ucArray09[9]) { for(temp09=1; temp09<15; temp09++) { if(ucArray09[temp09]>0) { if(ucArray09[temp09]==bit09) { temp09 = 99; break; } } } if(temp09>15) { continue; } // 2016/2/22 11:42 数组越界检查(超过14了) if((9+bit09+1)>14) { continue; } if(0==ucArray09[9+bit09+1]) { ucArray09[9] = bit09; ucArray09[9+bit09+1] = bit09; } } else { bit09 = 8; } // for(temp09=1; temp09<15; temp09++) // { // printf("%d=%d ", temp09, ucArray09[temp09]); // } // // printf("*9\n"); // 2016/2/21 22:08 第10数字 填字的话只剩下这种情况(最后五:31213):xxxxxxxxx31213 int bit10 = 0; // for(bit10=1; bit10<=7; bit10++) for(bit10=1; bit10<=3; bit10++) { unsigned char ucArray10[15] = {0}; int temp10 = 0; for(temp10=0; temp10<15; temp10++) { ucArray10[temp10] = ucArray09[temp10]; } if(0==ucArray10[10]) { for(temp10=1; temp10<15; temp10++) { if(ucArray10[temp10]>0) { if(ucArray10[temp10]==bit10) { temp10 = 99; break; } } } if(temp10>15) { continue; } // 2016/2/22 11:42 数组越界检查(超过14了) if((10+bit10+1)>14) { continue; } if(0==ucArray10[10+bit10+1]) { ucArray10[10] = bit10; ucArray10[10+bit10+1] = bit10; } } else { bit10 = 8; } // for(temp10=1; temp10<15; temp10++) // { // printf("%d=%d ", temp10, ucArray10[temp10]); // } // // printf("*10\n"); // 2016/2/21 22:15 第11数字 填字的话只剩下这种情况(最后四:2162):xxxxxxxxxx2162 int bit11 = 0; // for(bit11=1; bit11<=7; bit11++) for(bit11=1; bit11<=2; bit11++) { unsigned char ucArray11[15] = {0}; int temp11 = 0; for(temp11=0; temp11<15; temp11++) { ucArray11[temp11] = ucArray10[temp11]; } if(0==ucArray11[11]) { for(temp11=1; temp11<15; temp11++) { if(ucArray11[temp11]>0) { if(ucArray11[temp11]==bit11) { temp11 = 99; break; } } } if(temp11>15) { continue; } if((11+bit11+1)>14) { continue; } if(0==ucArray11[11+bit11+1]) { ucArray11[11] = bit11; ucArray11[11+bit11+1] = bit11; } else { continue; } } else { bit11 = 8; } for(temp11=1; temp11<12; temp11++) { if(0==ucArray11[temp11]) { temp11 = 88; break; } } if(temp11>15) { continue; } // for(temp11=1; temp11<15; temp11++) // { // printf("%d=%d ", temp11, ucArray11[temp11]); // } // // printf("*11\n"); // 2016/2/21 22:44 第12数字 填字的话只剩下这种情况(最后三:161):35743625427161 int bit12 = 0; // for(bit12=1; bit12<=7; bit12++) for(bit12=1; bit12<=1; bit12++) { unsigned char ucArray12[15] = {0}; int temp12 = 0; for(temp12=0; temp12<15; temp12++) { ucArray12[temp12] = ucArray11[temp12]; } if(0==ucArray12[12]) { // 2016/2/22 15:17 1617245263 4753 for(temp12=1; temp12<15; temp12++) { if(ucArray12[temp12]>0) { if(ucArray12[temp12]==bit12) { temp12 = 99; break; } } } if(temp12>15) { continue; } if((12+bit12+1)>14) { continue; } if(0==ucArray12[12+bit12+1]) { ucArray12[12] = bit12; ucArray12[12+bit12+1] = bit12; } else { continue; } } else { bit12 = 8; } // 2016/2/22 11:29 去掉 数字中有零的输出:35673415164700 // for(temp12=1; temp12<13; temp12++) for(temp12=1; temp12<15; temp12++) { if(0==ucArray12[temp12]) { temp12 = 88; break; } } if(temp12>15) { continue; } for(temp12=1; temp12<15; temp12++) { // printf("%d=%d ", temp12, ucArray12[temp12]); printf("%d", ucArray12[temp12]); } printf("*12\n"); } } } } } } } } } } } } return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值