- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在文件中编写一个名为 bitCount()
的函数:bitcount.c
返回其无符号整数参数的二进制表示形式的位数。
这是我目前所拥有的:
#include <stdio.h>
int bitCount (unsigned int n);
int main () {
printf ("# 1-bits in base 2 representation of %u = %d, should be 0\n",
0, bitCount (0));
printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
1, bitCount (1));
printf ("# 1-bits in base 2 representation of %u = %d, should be 16\n",
2863311530u, bitCount (2863311530u));
printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
536870912, bitCount (536870912));
printf ("# 1-bits in base 2 representation of %u = %d, should be 32\n",
4294967295u, bitCount (4294967295u));
return 0;
}
int bitCount (unsigned int n) {
/* your code here */
}
好的,当我运行它时,我得到:
# 1-bits in base 2 representation of 0 = 1, should be 0
# 1-bits in base 2 representation of 1 = 56, should be 1
# 1-bits in base 2 representation of 2863311530 = 57, should be 16
# 1-bits in base 2 representation of 536870912 = 67, should be 1
# 1-bits in base 2 representation of 4294967295 = 65, should be 32
RUN SUCCESSFUL (total time: 14ms)
它没有返回正确的位数。
在 C 中返回其无符号整数参数的二进制表示的位数的最佳方法是什么?
最佳答案
这是一个不需要迭代的解决方案。它利用了这样一个事实,即在二进制中添加位完全独立于位的位置,并且总和永远不会超过 2 位。 00+00=00
, 00+01=01
, 01+00=01
, 01+01=10
>。第一次加法同时添加 16 个不同的 1 位值,第二次添加 8 个 2 位值,之后的每个加法加法减半,直到只剩下一个值。
int bitCount(unsigned int n)
{
n = ((0xaaaaaaaa & n) >> 1) + (0x55555555 & n);
n = ((0xcccccccc & n) >> 2) + (0x33333333 & n);
n = ((0xf0f0f0f0 & n) >> 4) + (0x0f0f0f0f & n);
n = ((0xff00ff00 & n) >> 8) + (0x00ff00ff & n);
n = ((0xffff0000 & n) >> 16) + (0x0000ffff & n);
return n;
}
这是硬编码为 32 位整数,如果您的大小不同,则需要进行调整。
关于计算无符号整数中的位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14780928/
假设我得到了两个整数 a, b 其中 a 是一个正整数并且小于 b 。我必须找到一种有效的算法,它会在 [a, b] 区间内给出 base2 位数(位数)的总和。例如,在区间 [0, 4] 中,数字之
到目前为止我已经尝试过不同的 autofilter但非选项似乎对我有用,我有许可证号列,其中应该只有 10 位数字,并且 autofilter我正在尝试查找少于或多于 10 位数字的条目, 我将该列转
谁能告诉我检查输入的数字是否为 3 位数字的正则表达式...它也不应该允许字母.... 最佳答案 3 个数字的正则表达式为 ^[0-9]{3}$ 或 ^\d{3}$ 关于javascript - 是否
我不知道这在 SQL Server 中是否可行,但我得问问它 ;-) 我在表 work 中有一个名为 duty 的列。 假设 Work.Duty 包含不同的数字,例如 (1, 2, 3, 20, 22
我正在运行一个我创建的java程序,它存储用户输入的数据。具体来说,有 4 个数组列表,分别是songName、songArtist、songYear 和songAlbum。我有一个“songYear
我不知道这在 SQL Server 中是否可行,但我得问问它 ;-) 我在表 work 中有一个名为 duty 的列。 假设 Work.Duty 包含不同的数字,例如 (1, 2, 3, 20, 22
给定一个 float ,我想使用半偶数舍入将结果四舍五入到小数点后四位,即四舍五入到下一个偶数的方法。例如,当我有以下代码片段时: #include #include int main(){
有没有一种方法可以在不使用小程序的情况下确定客户端计算机上的 jvm 位数?我确实看到了这个link但这决定了 jvm 版本而不是位数。提前致谢 最佳答案 您可以尝试确定浏览器位数 - 32 位 ja
我正在编写一个程序来计算给定数字的两个二进制表示之间的共同位数。我写的代码是: int common_bits(int a, int b) { static long binaryNo1,binary
如何使表格中的每个单元格的最小宽度为 3 位数字并且不会更大?现在我正在对 min-width 进行硬编码,但我不喜欢对值进行硬编码,因为将来我可能想更改字体。如果需要 Javascript 也没关系
我正在尝试匹配后跟一个空格或制表符和 5 个数字的单词。例如 some noise text off 12345 some noise text again. 另一个例子是: Some noise t
我在从 double 到 string 的转换时遇到问题。 我要转换: double value: 0.0772486324655191 string value: 0.077248632465519
我正在尝试实现一个使用 128 位 key 的密码。 key 计划的一部分是将 key 向右旋转 29 位,但我不确定该怎么做,因为 Java 中没有单一数据类型可以保存整个 key 。我将它存储在两
我正在尝试在 AVX2 中对 128 位数进行左旋转。由于没有直接的方法,我尝试使用左移和右移来完成我的任务。 这是我执行相同操作的代码片段。 l = 4; r = 4
我有一个 int,它的值类似于 1235 和 12890。我只想要这个 int 的前 2 位数字。我怎样才能提取它? 想了半天,想不出什么办法。 最佳答案 减少数字,直到只剩下两位数: while (
TL:DR 我想要紧跟“+”符号的任何行上的前两个数字的语法。 给定以下文本(来自熟悉的实用程序): power_meter-acpi-0 Adapter: ACPI interface power1
因此根据 cplusplus.com,当您通过以下方式将输出流的格式标志设置为科学记数法时 of.setf(ios::scientific) 您应该在指数中看到 3 位加号和一个符号。但是,我的输出似
这个问题在这里已经有了答案: How can I pad a value with leading zeros? (77 个答案) 关闭 9 年前。 如果小时数小于 10 小时,则小时数通常以个位数
我正在使用 moment.js使用根据距离/速度计算的日期时间。我也在使用 moment duration format plugin .当我将值传递给 moment.duration 然后对其进行格
我正在将一个应用程序从 .NET 移植到 Mono 运行时,并且在代码中的某个位置我可以看到一个 float 具有值 158136.422。我对 float 的理解是它是 7 位精度,那么这个数字如何
我是一名优秀的程序员,十分优秀!