首页 > 学院 > 开发设计 > 正文

Training2:位操作训练

2019-11-14 09:03:54
字体:
来源:转载
供稿:网友

1. 题目:请编写一个c函数,该函数给出一个字节中被置为1的位的个数

/*****************************************************copyright (C), 2016-2017, Lighting Studio. Co.,     Ltd. File name:Author:luoye   Version:0.1    Date: Description:Funcion List: *****************************************************/#include <stdio.h>int main(){	int num, mask;	int count=0, i;	PRintf("Please enter a number: ");	scanf("%d",&num);	mask=1<<7;	for(i=0;i<8;i++)	{		if(num&mask)		{			count++;		}		num<<=1;	}	printf("The 1 number is %d/n",count);    return 0;}

2.题目:输入一个整数a,再输入两个整数p1,p2(p1,p2<32),输出该整数的二进制表示方法中从右端开始的p1p2.

/*****************************************************copyright (C), 2016-2017, Lighting Studio. Co.,     Ltd. File name:Author:luoye   Version:0.1    Date: Description:Funcion List: *****************************************************/#include <stdio.h>int main(){	int num, num1, num2, mask, i;	printf("Please enter a number:");	scanf("%d",&num);	printf("Please enter two number(number<32):");	scanf("%d%d",&num1,&num2);	mask = 1<< num2-1;	for(i = num1; i <= num2; i++)	{		putchar(num&mask ? '1' : '0');		mask >>= 1;	}	printf("/n");    return 0;}

3.题目:输入一个整数a,再输入两个整数p1,p2(p1,p2<32),将该整数的二进制表示方法中从右端开始的p1p2位取反后输出

/*****************************************************copyright (C), 2016-2017, Lighting Studio. Co.,     Ltd. File name:Author:luoye   Version:0.1    Date: Description:Funcion List: *****************************************************/#include <stdio.h>int main(){	int num, num1, num2, mask, i;	printf("Please enter a number:");	scanf("%d",&num);	printf("Please enter two number(number<32):");	scanf("%d%d",&num1,&num2);	mask = 1<< num2-1;	for(i = num1; i <= num2; i++)	{		putchar( num & mask ? '0' : '1');		mask >>= 1;	}	printf("/n");    return 0;}

4. 题目:输入一个整数a,再输入两个整数p(p<32),v(0|1),将该整数a的p位设置为v,输出修改后的该整数的二进制表示.

/*****************************************************copyright (C), 2016-2017, Lighting Studio. Co.,     Ltd. File name:Author:luoye   Version:0.1    Date: Description:Funcion List: *****************************************************/#include <stdio.h>int main(){	int num, num1, num2, i;	int mask_0, mask_1, mask;	printf("Please enter a number: ");	scanf("%d",&num);	printf("Please enter a number(number<32): ");	scanf("%d",&num1);	printf("Please enter a number(number 0|1): ");	scanf("%d",&num2);	if( num2 == 1)	{		mask_1 = 1<<num1-1;  //生成一个指定位数1,其他位数为0的掩码		num = num | mask_1;  	}	else	{		mask_0 = 1<<num1-1;  		mask_0 = ~mask_0;     //生成一个指定位数为0,其他位数为1的掩码		num = num & mask_0;	}	mask =1<<31;	for(i=0;i<32;i++)	{		putchar( num & mask ? '1' : '0');		num <<=1;	}	printf("B/n");    return 0;}

5.题目:输入一个32位的整数a,使用按位异或^运算,生成一个新的32位整数b,使得该整数b的每一位等于原整数a中该位左右两边两个bit位的异或结果。

提示:0 ^ 0 = 0; 1 ^ 1 = 0; 0 ^ 1 = 1; 1 ^ 0 = 1;

/*****************************************************copyright (C), 2016-2017, Lighting Studio. Co.,     Ltd. File name:Author:luoye   Version:0.1    Date: Description:Funcion List: *****************************************************/#include <stdio.h>int main(){	int num, num1, mask, mask_p, i;   //num为原来的数  num1为最后得出的数	int temp[34];	int sum = 1;	temp[0] = 0;            //默认32位的前面后面都为0	temp[33] = 0;	printf("Please enter a number: /n");	scanf("%d",&num);	mask = 1<<31;	for( i = 1; i <= 32; i++)	{		temp[i] = (num & mask ? 1 : 0 );		num <<=1;	}//  输出原数的二进制表示//	for( i = 0; i < 34; i++)//	{//		printf("%d",temp[i]);//	}//	printf("/n");//相邻两位取异或	num1 = 0;	for( i = 32; i >=1; i--)	{		num1 += (temp[i - 1] ^ temp [i+1]) * sum;		sum *= 2 ;             //sum表示二的次方用来得到结果数b	}	mask_p = 1<<31;	for( i = 0; i < 32; i++)	{		putchar(num1 & mask_p ? '1' : '0');    //输出结果数的二进制表示		num1 <<=1;	}	printf("/n");    return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表