# Binary representation in C

This is a discussion on Binary representation in C within the C Programming forums, part of the General Programming Boards category; We have one for hex (0x), but what is the standard representation for binary numbers in C? The C99 standard ...

1. ## Binary representation in C

We have one for hex (0x), but what is the standard representation for binary numbers in C?
The C99 standard makes no mention of it, and google seems to corroborate that there is no standard representation. Why allow hex notation but not binary?

2. There isn't one (I don't think), Humans have no need to work in binary, only the computer does, why try and make it more complex for the humans? You've already got decimal and hex, not to mention you've got bitwise ops

You could always make your own.

3. Don't forget C also supports octal

4. even octal too eh... now binary really feels left out

I think in some circumstances hex would be more complex. i.e. when setting up OR-flags, its easier to see that none collide by specifying them in binary.

5. Originally Posted by @nthony
even octal too eh... now binary really feels left out
might be C knows what exactly you are not looking for

Originally Posted by @nthony
I think in some circumstances hex would be more complex. i.e. when setting up OR-flags, its easier to see that none collide by specifying them in binary.
why would you even need to go to hex or for that matter any base? just take baste 10 and make simple operation like OR
Ex: (2 | 3) this is equivalent to (binary(2) OR binary(3))

6. Some compilers which target embedded processors support the ability to write binary constants (IIRC, the Kiel C compiler does).

7. Originally Posted by gibsosmat
might be C knows what exactly you are not looking for
why would you even need to go to hex or for that matter any base? just take baste 10 and make simple operation like OR
Ex: (2 | 3) this is equivalent to (binary(2) OR binary(3))
To determine that you are using unique bits. Think about it: whats easier to detect bit collisions in "3" or "101"? Sure, you can just remember to "always use powers of 2", but its a lot more clear when you can easily spot when exponents are used twice:
Code:
```00001
00010
00100
01010  /* at first glance, you can easily spot the repeated 1 */
10000

versus:
1
2
4
10      /* at first glance, this is not as obvious */
16```
Originally Posted by Salem
Some compilers which target embedded processors support the ability to write binary constants (IIRC, the Kiel C compiler does).
Interesting... I guess that would explain why I've seen some notations (I think it was in the form of a "b" suffix or prefix) being used to specify a binary number (in fact, up until I actually tried using it, I had just thought it was part of the standard).

8. Alternatively, you can set it up like this:
Code:
```#define Bit(n) (1 << (n))
#define Bit0 Bit(0)
#define Bit1 Bit(1)
....
#define Bit4 Bit(4)```
You can of course also use it more directly:
Code:
`   if (val & (1 << 4))  ...`
--
Mats

9. This is for single bits... but I mean he wants to do something like:
Code:
`selective = number & 10101010010101011010101001010101b;`

10. Originally Posted by @nthony
We have one for hex (0x), but what is the standard representation for binary numbers in C?
The C99 standard makes no mention of it, and google seems to corroborate that there is no standard representation. Why allow hex notation but not binary?
Because hex notation is equivalent to binary and far more convenient. Memorize this table:

0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
A = 1010
B = 1011
C = 1100
D = 1101
E = 1110
F = 1111

Suppose you had a binary value 10110110. That's 1011 0110, which is 0xB6 from the above table.

Suppose you had a hex value A43C. That's 1010 0100 0011 1100 in binary again using the table.

The two bases are so conveniently related to each other this way that it's pointless to support both. Why type 16 binary digits when you could type 4 hex digits? And don't object to having to memorize it -- this is C, it's something you absolutely need to know.

11. Originally Posted by maxorator
This is for single bits... but I mean he wants to do something like:
Code:
`selective = number & 10101010010101011010101001010101b;`
Code:
```#define b0000 0x0
#define b0001 0x1
#define b0010 0x2
#define b0011 0x3
#define b0100 0x4
#define b0101 0x5
#define b0110 0x6
#define b0111 0x7
#define b1000 0x8
#define b1001 0x9
#define b1010 0xA
...```

12. Originally Posted by @nthony
even octal too eh... now binary really feels left out
Again, there is a table.

0 = 000
1 = 001
2 = 010
3 = 011
4 = 100
5 = 101
6 = 110
7 = 111

Converting from octal/hex/binary is so trivial that it's pointless to support all three. Binary, being the most verbose, got left out.

13. and most important of all - the binary table:

0 = 0
1 = 1

that was easy. =)

seriously, though - binary representation deserves a place in the language. it's a real shame it was left out of the standard...

14. >that was easy. =)
Uh, can you describe that a different way? I didn't get it.

>binary representation deserves a place in the language
Why? Hexadecimal is just as easy and far less verbose.

15. Originally Posted by maxorator
This is for single bits... but I mean he wants to do something like:
Code:
`selective = number & 10101010010101011010101001010101b;`
Code:
`selective = number & 0xAA55AA55;`
(At first I thought it was AAAAAAAA - but scanning more carefully I came up wtih that - I think it's correct). I'd hate to see number being 12345678 hex in the debugger, and then having to disassemble the source code to see what the binary patter represents in hex - converting hex to binary and binary to hex will become second nature if you do this for a while. Before it's secodn nature, just use the table posted.

And of course, the risk of making a mistake by adding one too many or too few digits is less when you have 8 than when you have 32. For small numbers, I would agree that binary may make some sense. But for 32-bit numbers it gets VERY unwieldy - not to mention 64-bit numbers. Even hex gets a bit messy at 16 digits.

--
Mats

Page 1 of 2 12 Last
Popular pages Recent additions