0

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; } 
4
  • Have you tried stepping through the code line by line in a debugger? Commented Jan 26, 2017 at 5:24
  • Also, remember that bit numbers are zero-based. The first bit is number 0, the last bit in a 32-bit number is number 31. Commented Jan 26, 2017 at 5:25
  • how do you do that? Commented Jan 26, 2017 at 5:25
  • ahhh, nevermind. Stupid for loop was throwing me off. Thanks for the hints Commented Jan 26, 2017 at 5:28

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.