将整数转换为二进制并存储在指定大小的整数数组中:c++

2022-01-09 00:00:00 binary c++

我想将整数转换为二进制字符串,然后将整数字符串的每一位存储到给定大小的整数数组的元素中.我确信输入整数的二进制表达式不会超过指定数组的大小.如何在 C++ 中做到这一点?

I want to convert an integer to binary string and then store each bit of the integer string to an element of a integer array of a given size. I am sure that the input integer's binary expression won't exceed the size of the array specified. How to do this in c++?

推荐答案

伪代码:

int value = ????  // assuming a 32 bit int
int i;

for (i = 0; i < 32; ++i) {
    array[i] = (value >> i) & 1;
}

相关文章