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

后端 未结 8 1116
时光说笑
时光说笑 2020-11-30 17:36

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?

OK, a loop is

8条回答
  •  悲&欢浪女
    2020-11-30 17:38

    Here's a very simple way to do it;

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

提交回复
热议问题