2

I am using this code

int get_bit(int n, int bitnr) {
int mask = 1 << bitnr;
int masked_n = n & mask;
int thebit = masked_n >> bitnr;
return thebit;
}

void printbits(uint32_t bits) {
    int i;
    for (i = 0; i < 32; i++)
        printf("%d", get_bit(bits, i));
    printf("\n");
}

to get and print the bits of a uint32_t, and in another function this code

uint32_t bits= 0;
bits|= 1<< 0;

to change the most significant bit (left-most) from 0 to 1.

the problem is when printing bits using the printbits function, it prints them right, but when using printf("%#x", bits); I'm getting the hex value of the bits as if they are read from right to left!

so the printbits gives me '10000000000000000000000000000000' but the hex value printed is the value of '00000000000000000000000000000001'.

Help appreciated

codeoverflow
  • 143
  • 2
  • 12

4 Answers4

2

 Changing the Most significant bit:

This line:

bits |= 1<< 0;

changes the least significant bit (LSB). 1 << 0 equals 1, which is not very significant :).

But, if you do:

bits |= 1 << 31;

or

bits |= 0x80000000;

You would actually change the most significant bit (MSB).

Printing the number in binary:

Your code is actually printing the number from right to left. You must change your loop to decrement.

for (i = 31; i >= 0; i--)

And if printing in the wrong way is fine for you (who knows...), try this:

uint32_t n = 41;
while (n) {
    printf("%d", n & 1);
    n >>= 1;
}

This can be easily adapted to print in the correct way using a recursive function:

void printbits(uint32_t n) {
    if (n) {
        printbits(n >> 1);
        printf("%d", n & 1);
    }
}

This algorithm works for any base with minor modifications.

Community
  • 1
  • 1
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
1

This code bits|= 1<< 0; sets the least not the most significant bit in an integer. Same goes true for your get_bit function - it numbers the bits from right to left.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

(1 << 0) is the least significant bit. (1 << 31) would be the most significant bit.

Your print function is printing bits in ascending order (wrong way). You need to reverse your for loop:

for (i = 31; i >= 0; i--)
user694733
  • 15,208
  • 2
  • 42
  • 68
0

This is changing the first digit on the right (least important bit) :

 uint32_t bits= 0;
 bits|= 1<< 0;

Use something like :

uint32_t bits= 0;
bits |= 0x8000;
Joze
  • 1,285
  • 4
  • 19
  • 33