| 題目:學(xué)習(xí)使用按位取反~! 1.程序分析:~0=1; ~1=0; 2.程序源代碼: #i nclude "stdio.h" main() { int a,b; a=234; b=~a; printf("\40: The a''s 1 complement(decimal) is %d \n",b); a=~a; printf("\40: The a''s 1 complement(hexidecimal) is %x \n",a); }
題目:取一個(gè)整數(shù)a從右端開始的4~7位。 程序分析:可以這樣考慮: (1)先使a右移4位。 (2)設(shè)置一個(gè)低4位全為1,其余全為0的數(shù)。可用~(~0<<4) (3)將上面二者進(jìn)行&運(yùn)算。 2.程序源代碼: main() { unsigned a,b,c,d; scanf("%o",&a); b=a>>4; c=~(~0<<4); d=b&c; printf("%o\n%o\n",a,d); } 題目:學(xué)習(xí)使用按位異或 ^ 。 1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0 2.程序源代碼: #i nclude "stdio.h" main() { int a,b; a=077; b=a^3; printf("\40: The a & b(decimal) is %d \n",b); b^=7; printf("\40: The a & b(decimal) is %d \n",b); }
| 題目:學(xué)習(xí)使用按位或 。 1.程序分析:0 0=0; 0 1=1; 1 0=1; 1 1=1 2.程序源代碼: #i nclude "stdio.h" main() { int a,b; a=077; b=a 3; printf("\40: The a & b(decimal) is %d \n",b); b =7; printf("\40: The a & b(decimal) is %d \n",b); }
題目:學(xué)習(xí)使用按位與 & 。 1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1 2.程序源代碼: #i nclude "stdio.h" main() { int a,b; a=077; b=a&3; printf("\40: The a & b(decimal) is %d \n",b); b&=7; printf("\40: The a & b(decimal) is %d \n",b); }
| |