C语言 如何从 C 中的整数值获取逐位数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2249731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:27:35  来源:igfitidea点击:

How do I get bit-by-bit data from an integer value in C?

cbit-manipulation

提问by Badr

I want to extract bits of a decimal number.

我想提取十进制数的位。

For example, 7 is binary 0111, and I want to get 0 1 1 1 all bits stored in bool. How can I do so?

例如,7 是二进制 0111,我想得到 0 1 1 1 存储在 bool 中的所有位。我怎么能这样做?

OK, a loop is not a good option, can I do something else for this?

好的,循环不是一个好的选择,我可以为此做点别的吗?

回答by forefinger

If you want the k-th bit of n, then do

如果你想要 n 的第 k 位,那么做

(n & ( 1 << k )) >> k

Here we create a mask, apply the mask to n, and then right shift the masked value to get just the bit we want. We could write it out more fully as:

在这里,我们创建了一个掩码,将掩码应用于 n,然后将掩码值右移以获得我们想要的位。我们可以更完整地将其写成:

    int mask =  1 << k;
    int masked_n = n & mask;
    int thebit = masked_n >> k;

You can read more about bit-masking here.

您可以在此处阅读有关位屏蔽的更多信息。

Here is a program:

这是一个程序:

#include <stdio.h>
#include <stdlib.h>

int *get_bits(int n, int bitswanted){
  int *bits = malloc(sizeof(int) * bitswanted);

  int k;
  for(k=0; k<bitswanted; k++){
    int mask =  1 << k;
    int masked_n = n & mask;
    int thebit = masked_n >> k;
    bits[k] = thebit;
  }

  return bits;
}

int main(){
  int n=7;

  int  bitswanted = 5;

  int *bits = get_bits(n, bitswanted);

  printf("%d = ", n);

  int i;
  for(i=bitswanted-1; i>=0;i--){
    printf("%d ", bits[i]);
  }

  printf("\n");
}

回答by Joe

As requested, I decided to extend my comment on forefinger's answer to a full-fledged answer. Although his answer is correct, it is needlessly complex. Furthermore all current answers use signed ints to represent the values. This is dangerous, as right-shifting of negative values is implementation-defined (i.e. not portable) and left-shifting can lead to undefined behavior (see this question).

根据要求,我决定将我对食指答案的评论扩展为一个成熟的答案。虽然他的回答是正确的,但它是不必要的复杂。此外,所有当前的答案都使用带符号的ints 来表示值。这是危险的,因为负值的右移是实现定义的(即不可移植),而左移会导致未定义的行为(请参阅此问题)。

By right-shifting the desired bit into the least significant bit position, masking can be done with 1. No need to compute a new mask value for each bit.

通过将所需位右移到最低有效位位置,可以使用 来完成屏蔽1。无需为每一位计算新的掩码值。

(n >> k) & 1

As a complete program, computing (and subsequently printing) an array of single bit values:

作为一个完整的程序,计算(并随后打印)单个位值的数组:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
    unsigned
        input = 0b0111u,
        n_bits = 4u,
        *bits = (unsigned*)malloc(sizeof(unsigned) * n_bits),
        bit = 0;

    for(bit = 0; bit < n_bits; ++bit)
        bits[bit] = (input >> bit) & 1;

    for(bit = n_bits; bit--;)
        printf("%u", bits[bit]);
    printf("\n");

    free(bits);
}

Assuming that you want to calculate all bits as in this case, and not a specific one, the loop can be further changed to

假设您想像这种情况一样计算所有位,而不是特定的位,则可以进一步将循环更改为

for(bit = 0; bit < n_bits; ++bit, input >>= 1)
    bits[bit] = input & 1;

This modifies inputin place and thereby allows the use of a constant width, single-bit shift, which may be more efficient on some architectures.

input进行了适当的修改,从而允许使用恒定宽度的单位移位,这在某些架构上可能更有效。

回答by wallyk

Here's one way to do it—there are many others:

这是一种方法 - 还有许多其他方法:

bool b[4];
int v = 7;  // number to dissect

for (int j = 0;  j < 4;  ++j)
   b [j] =  0 != (v & (1 << j));

It is hard to understand why use of a loop is not desired, but it is easy enough to unroll the loop:

很难理解为什么不希望使用循环,但展开循环很容易:

bool b[4];
int v = 7;  // number to dissect

b [0] =  0 != (v & (1 << 0));
b [1] =  0 != (v & (1 << 1));
b [2] =  0 != (v & (1 << 2));
b [3] =  0 != (v & (1 << 3));

Or evaluating constant expressions in the last four statements:

或者评估最后四个语句中的常量表达式:

b [0] =  0 != (v & 1);
b [1] =  0 != (v & 2);
b [2] =  0 != (v & 4);
b [3] =  0 != (v & 8);

回答by d3vdpro

Here's a very simple way to do it;

这是一个非常简单的方法;

int main()
{
    int s=7,l=1;
    vector <bool> v;
    v.clear();
    while (l <= 4)
    {
        v.push_back(s%2);
        s /= 2;
        l++;
    }
    for (l=(v.size()-1); l >= 0; l--)
    {
        cout<<v[l]<<" ";
    }
    return 0;
}

回答by xinthose

@prateek thank you for your help. I rewrote the function with comments for use in a program. Increase 8 for more bits (up to 32 for an integer).

@prateek 感谢您的帮助。我用注释重写了该函数,以便在程序中使用。增加 8 以获得更多位(整数最多增加 32)。

std::vector <bool> bits_from_int (int integer)    // discern which bits of PLC codes are true
{
    std::vector <bool> bool_bits;

    // continously divide the integer by 2, if there is no remainder, the bit is 1, else it's 0
    for (int i = 0; i < 8; i++)
    {
        bool_bits.push_back (integer%2);    // remainder of dividing by 2
        integer /= 2;    // integer equals itself divided by 2
    }

    return bool_bits;
}

回答by wildplasser

If you don't want any loops, you'll have to write it out:

如果您不想要任何循环,则必须将其写出:

#include <stdio.h>
#include <stdbool.h>

int main(void)
{
    int num = 7;

    #if 0
        bool arr[4] = { (num&1) ?true: false, (num&2) ?true: false, (num&4) ?true: false, (num&8) ?true: false };
    #else
        #define BTB(v,i) ((v) & (1u << (i))) ? true : false
        bool arr[4] = { BTB(num,0), BTB(num,1), BTB(num,2), BTB(num,3)};
        #undef BTB
    #endif

    printf("%d %d %d %d\n", arr[3], arr[2], arr[1], arr[0]);

    return 0;
}

As demonstrated here, this also works in an initializer.

如此处所示,这也适用于初始化程序。

回答by Smit Ycyken

Using std::bitset

使用 std::bitset

int value = 123;
std::bitset<sizeof(int)> bits(value);
std::cout <<bits.to_string();

回答by anon

#include <stdio.h>

int main(void)
{
    int number = 7; /* signed */
    int vbool[8 * sizeof(int)];
    int i;
        for (i = 0; i < 8 * sizeof(int); i++)
        {
            vbool[i] = number<<i < 0;   
            printf("%d", vbool[i]);
        }
    return 0;
}