位操作 - 如何在 C++ 中计算二进制位
bit manipulation - how to calculate binary bits in c++?
如何在 c++ 中将数字的位设置为 ON例如:
对于 64 位平台上的数字 0:位是
00000000 00000000 00000000 00000000对于 64 位平台上的数字 2000000000:位是
01110111 00110101 10010100 00000000
这是我到目前为止的代码:
#include<iostream>
#include<limit.h>
const int BIT = CHAR_BIT * CHAR_BIT; // CHAR_BIT states the architecture of platform
// 8 byte(64 bit) 4 byte(32 bit)
int main()
{
char* bit = new char[BIT + 1];
int i;
for ( i = 0; i < BIT + 1; i++)
bit[i] = '0';
unsigned int x;
cout << "Enter number";
cin >> x;
if (x == /* some value */)
{
// then calculate which zero's should turn to one as per number
// or how can i do this using loops
}
bit[i] = ' ';
// displays bit in a specific format
for (i = 0; i < BIT; i++)
{
if ((i % 8) == 0)
cout << " ";
cout << bit[i];
}
}
请阅读此 http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary#dec2bin。它既有算法又有样本。快乐学习:)。
while (x != 0)
{
std::cout << (x % 2);
x /= 2;
}
相关文章:
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 计算二进制文件中的位数
- 计算大二进制矩阵中小二进制矩阵的出现
- 如何生成统一的随机二进制数以成对计算C++中的汉明距离?
- 在 64 位 Linux armv8 计算机上编译 32 位二进制文件时遇到问题
- 在计算负数的二进制补码时如何实现"+1"加法
- C/C++ 方法中最快/最短计算二进制数字总和/又名二进制中的 1 数
- 手动将二进制表达式计算为十六进制
- 编译后如何计算某些二进制功能(或基本块)的校验和
- 以二进制形式计算连续的 1
- 计算每个子树(二进制搜索树)具有特定属性的叶子的数量
- 如何在没有循环的二进制数字表示中只计算1位
- Wxwidgets 在另一台计算机上运行二进制文件时出现问题
- 为什么这个计算二进制搜索树中节点的递归函数总是返回比预期更大的结果
- 分别计算左右孩子的二进制树
- 我应该如何存储和计算二进制代码之间的汉明距离
- c++计算二进制值
- 为什么我在计算二进制数的补码时会出现分割错误?
- 如何计算二进制SHA256哈希为给定的十六进制数据在c++
- 位操作 - 如何在 C++ 中计算二进制位