Reputation: 105
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;
}
Upvotes: 0
Views: 932
Reputation: 1305
There is logic missing in your code..
Use
for(i **= 31**; i **>=** 0; i--)
instead of
for(i **= 32**; i **>** 0; i--)
Comment if it works for you
Upvotes: 1