How to get bitwise data from an integer value in C?

I want to extract the bits of a decimal number.

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

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

+92
c bit-manipulation
Feb 12 2018-10-12T00
source share
8 answers

If you want the kth bit n then do

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

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

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

Read more about bit masks here .

Here is the 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"); } 
+138
Feb 12 '10 at 4:54
source share

In accordance with the request, I decided to extend my comment on the index finger to a complete answer. Although his answer is correct, he is overly complex. In addition, all current answers use a signed int to represent the values. This is dangerous, since shifting negative values ​​according to the implementation (i.e. Not portable) and shifting to the left can lead to undefined behavior (see this question ).

By right shifting the desired bit to the least significant bit position, masking can be done with 1 . There is no need to calculate a new mask value for each bit.

 (n >> k) & 1 

As a complete program, the calculation (and subsequent printing) of an array of one-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 the bits, as in this case, and not a specific one, the cycle can be further changed to

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

This changes the input in place and thus allows the use of a constant width, single-bit shift, which may be more efficient for some architectures.

+66
Oct 07 '14 at 7:16
source share

Here is one way to do this - 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’s hard to understand why using a loop is undesirable, but just starting a loop is enough:

 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 by calculating 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); 
+4
Feb 12 '10 at 4:55
source share

Here is a very simple way to do this;

 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; } 
+3
Feb 12 '10 at 5:02
source share

If you do not need any loops, you will have to write:

 #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 shown here, this also works in the initializer.

+1
May 20 '13 at 11:17
source share

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

 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 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; } 
+1
Feb 24 '15 at 18:52
source share

Using std::bitset

 int value = 123; std::bitset<sizeof(int)> bits(value); std::cout <<bits.to_string(); 
+1
May 12 '17 at 2:47 p.m.
source share
 #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; } 
0
May 20 '13 at 2:55
source share



All Articles