how to loop through the digits of a binary number?
Solution 1
int n = 0x5b; // 1011011
Really you should just do this, hexadecimal in general is much better representation:
printf("%x", n); // this prints "5b"
To get it in binary, (with emphasis on easy understanding) try something like this:
printf("%s", "0b"); // common prefix to denote that binary follows
bool leading = true; // we're processing leading zeroes
// starting with the most significant bit to the least
for (int i = sizeof(n) * CHAR_BIT - 1; i >= 0; --i) {
int bit = (n >> i) & 1;
leading |= bit; // if the bit is 1, we are no longer reading leading zeroes
if (!leading)
printf("%d", bit);
}
if (leading) // all zero, so just print 0
printf("0");
// at this point, for n = 0x5b, we'll have printed 0b1011011
Solution 2
Expanding on Frédéric and Gabi's answers, all you need to do is realise that the rules in base 2 are no different to in base 10 - you just need to do your division and modulus with a divisor 2 instead of 10.
The next step is simply to use number >> 1
instead of number / 2
and number & 0x1
instead of number % 2
to improve performance. Mind you, with modern optimising compilers there's probably no difference...
Solution 3
You can use modulo and division by 2 exactly like you would in base 10. You can also use binary operators, but if you already know how to do that in base 10, it would be easier if you just used division and modulo
Solution 4
Use an AND with increasing powers of two...
Solution 5
In C, at least, you can do something like:
while (val != 0)
{
printf("%d", val&0x1);
val = val>>1;
}
Attilah
Updated on June 04, 2022Comments
-
Attilah almost 2 years
I have a binary number 1011011, how can I loop through all these binary digits one after the other ?
I know how to do this for decimal integers by using modulo and division.
-
Attilah over 13 yearsthanks, i just realized that i could do modulo and division by 2.
-
Gabi Purcaru over 13 years@Attilah you can do that in any base actually, even base 16 (but you would have to manually replace 10 with A and so on) :)
-
Matt Joiner over 13 yearsNote in advance: You want to include limits.h, stdbool.h (requires C99), stdio.h if you try to build this. If these aren't available for whatever reason, let me know and I'll modify the code to conform to your respective system.
-
R.. GitHub STOP HELPING ICE over 13 yearsThere is a difference if
number
is signed. The compiler has to go to extra trouble to meet C's inept definition of division ((-3)/2==-1
, not-2
like it should). Of course in many cases you should fix this just by using unsigned types rather than resorting to bitwise operations, but if your value really is signed and you wantnumber%2
to yield -2 whennumber
is -3,number&1
is the way to go. Keep in mind bitshifts are not well-defined for negative numbers though. -
Mac over 13 yearsRight you are! Good point... I have a bad habit of thinking unsigned when dealing with bit twidling.
-
Nathan S. over 13 yearsNote that you can just say printf("0b"); no need for the %s and string argument.