So i'm trying to extracts bits from a 32=bit binary integer using bit shifting and masking, but i'm sightly off by one.
#include <stdio.h> int main (){ int number = 87; //0000 000 0000 0000 0000 0000 0101 0111 int i; int bit; int g = 0; for(i = 32; i > 0; i--){ if(g%4==0 && g!=0){ printf(" "); } g++; bit = (number >> i) & 1; printf("%d", bit); } printf("\n"); return 0; }
0, the last bit in a 32-bit number is number31.