正在确定字符中的设置位数
Determining number of set bits in a char
如果每个单元存储为一个设置位,则该程序应确定变量c_val
的值中存储了多少个单元。我的问题是:为什么作者写了if (c % 2 == 1) count++;
,然后用这个语句c = c >> 1;
将c
向右移动?
#include <stdio.h>
#include <cstdlib>
int main(){
unsigned char c_val;
printf("char value = ");
scanf("%c", &c_val);
int count = 0;
unsigned char c = c_val;
while(c){
if (c % 2 == 1) count++;
c = c >> 1;
}
printf("%d bits are set", count);
system("pause");
}
类型char的数据大小总是一个字节-没有例外。然而,此代码计算c_val
中的popcount,即1位的数量。
我们可以从翻译相关代码
while (c) {
if (c % 2 == 1) count++;
c = c >> 1;
}
至
while (c != 0) {
if (c & 0x1 == 1) count++; /* if the rightmost bit of c is 1, then count++ */
c = c / 2;
}
我所做的最后一个更改之所以有效,是因为对无符号整数数据类型(在本例中为unsigned char
)进行右移相当于除以2,语义为向零取整。
我们可以把c
看作是比特的传送带——在每次循环迭代中,零个比特从左边进来,一个比特从右边掉下来。如果最右边的位是1,我们将计数增加1,否则计数保持不变。因此,一旦c
被零比特填充,我们就知道我们已经计算了所有的一比特,并且正是一比特,所以count
包含c_val
中的一比特的数量。
这根本不是一个确定char
类型实例"大小"的函数,而是一个字符中设置为1的位数。
表达式
c % 2 == 1
确定最低有效位是否是1。
移位将倒数第二个钻头带入最后一个位置,以便对其进行测试。
条件while (c)
意味着继续计数1并移位,直到整个字节都为零。
您的代码只是对char c中的1位进行编码。"c%2===1"检查"c"中的最后一位是否为1。因此,我们必须使用"c=c>>1"将"c"中的其他位移到最后一个位置。
其他相同的方法:
#include <stdio.h>
#include <conio.h>
unsigned int f (unsigned int a , unsigned int b);
unsigned int f (unsigned int a , unsigned int b)
{
return a ? f ( (a&b) << 1, a ^b) : b;
}
int bitcount(int n) {
int tot = 0;
int i;
for (i = 1; i <= n; i = i<<1)
if (n & i)
++tot;
return tot;
}
int bitcount_sparse_ones(int n) {
int tot = 0;
while (n) {
++tot;
n &= n - 1;
}
return tot;
}
int main()
{
int a = 12;
int b = 18;
int c = f(a,b);
printf("Sum = %dn", c);
int CountA = bitcount(a);
int CountB = bitcount(b);
int CntA = bitcount_sparse_ones(a);
int CntB = bitcount_sparse_ones(b);
printf("CountA = %d and CountB = %dn", CountA, CountB);
printf("CntA = %d and CntB = %dn", CntA, CntB);
getch();
return 0;
}
相关文章:
- 对字符数组中的元素执行逐位操作
- 位移操作和位掩码未检测到重复字符
- C++:使用没有位移位的指针将无符号字符转换为无符号 int
- 追加位以增加字符的大小
- 从 C++ 中的位表示形式初始化字符
- 如何将32位字符与内联assembelyc++中的32位字符进行比较
- 循环访问 C++ 个字符数组的位
- 在 C++ 中从 8 位 ASCII 字符创建 7 位 ASCII 文本字符串
- 对有符号字符的按位运算
- Visual C 32位整数从文件到8位字符到文件 - 程序在某些整数上崩溃
- 为什么字符指针会得到一个 32 位值 (ffffff88)
- 如何将 16 位无符号 int 转换为 8 位无符号字符并最终返回无符号字符*?
- 将转义的 UTF-8 八位字节的字符数组转换为 C++ 的字符串
- 外置"C"字符表的区别[256][256];和外字符表[256][256];
- 短(ASCII,每个字符 7 位)字符串存储和C++中的比较优化
- 为什么我的编译器在 64 位计算机上运行时使用 8 位字符?
- 如何读取和重置字符位
- scanf 只能将 3 个字符读取到 5 位字符数组中
- 为什么 printf 将 8 位字符填充到 32 位
- 8个8位字符的数组图