Representing binary values in C ?
Representing binary values in C ?
..
Last edited by Perica on Sun Dec 03, 2006 9:18 pm, edited 1 time in total.
Re:Representing binary values in C ?
There are two answers to this question.
The first is 'No'.
The second is 'It depends on how chummy you want to get with the preprocessor'.
What you can do is define a bunch of macros
#define bits_0000 0
#define bits_0001 1
#define bits_0010 2
...
#define bits_1111 15
#define BINARY_1BYTE(x1, x2) ((bits_##x1 << 4) | bits_##x2)
Then you can define a single binary byte as BINARY_1BYTE(0101, 1010) or something. Elaborate as suits your fancy, or just use hexadecimal constants like everyone else does.
(Actually, there's another trick, but it requires a space between -every- bit. And you have to use single characters for the bits. And those characters are macros. It's not worth using unless you're initializing a black and white bitmap by hand.)
Hope this helps.
--Jamethiel
The first is 'No'.
The second is 'It depends on how chummy you want to get with the preprocessor'.
What you can do is define a bunch of macros
#define bits_0000 0
#define bits_0001 1
#define bits_0010 2
...
#define bits_1111 15
#define BINARY_1BYTE(x1, x2) ((bits_##x1 << 4) | bits_##x2)
Then you can define a single binary byte as BINARY_1BYTE(0101, 1010) or something. Elaborate as suits your fancy, or just use hexadecimal constants like everyone else does.
(Actually, there's another trick, but it requires a space between -every- bit. And you have to use single characters for the bits. And those characters are macros. It's not worth using unless you're initializing a black and white bitmap by hand.)
Hope this helps.
--Jamethiel
Re:Representing binary values in C ?
..
Last edited by Perica on Sun Dec 03, 2006 9:19 pm, edited 1 time in total.
Re:Representing binary values in C ?
I'd personally learn to translate hexadecimals to binary on the fly and use them instead of binary. It's not hard, and it's faster when you know how to do it, since there's less counting on the amount of characters. 0x80000001 is much easier to read than 10000000000000000000000000000001 after all.
Also realize that if you are dealing with bitmasks, you can just define a macro or function to set one bit, and rely on compiler to optimize it away.
Also realize that if you are dealing with bitmasks, you can just define a macro or function to set one bit, and rely on compiler to optimize it away.
Code: Select all
/* if make a macro like this: */
#define BIT(n) (1 << (n))
/* with any optimizing compiler this: */
BIT(1) | BIT(24) | BIT(15);
/* should result in the same compiled code as this: */
0x01008002;
Re:Representing binary values in C ?
The good news is, when working in your head, it is quite easy to isolate bits in hex. First, break each byte down to it's individual nybbles.
Now, for each bit b, all you need to do is use the b power of 2 (assuming zero-indexed bit count). Thus,
0001 = bit 0 = 2 ^ 0 = 1
0010 = bit 1 = 2 ^ 1 = 2
0100 = bit 2 = 2 ^ 2 = 4
1000 = bit 3 = 2 ^ 3 = 8
Add the appropriate values together to get the value of the nybble.
So, if you want to get the bit mask 00101101, you would first
break the nybbles apart:
0010 1101
then for the high-order nybble, the value is
2^1 = 2
and the value of the low-order nybble is
2^0 = 1
2^ 2 = 4
2^ 3 = 8
8 + 4 + 1 = D
now recombine the nybbles to get 0x2D. HTH.
Of course, it could be pointed out that most systems have a calculator utility which will convert hex to binary, anyway. Oh, well.
Now, for each bit b, all you need to do is use the b power of 2 (assuming zero-indexed bit count). Thus,
0001 = bit 0 = 2 ^ 0 = 1
0010 = bit 1 = 2 ^ 1 = 2
0100 = bit 2 = 2 ^ 2 = 4
1000 = bit 3 = 2 ^ 3 = 8
Add the appropriate values together to get the value of the nybble.
So, if you want to get the bit mask 00101101, you would first
break the nybbles apart:
0010 1101
then for the high-order nybble, the value is
2^1 = 2
and the value of the low-order nybble is
2^0 = 1
2^ 2 = 4
2^ 3 = 8
8 + 4 + 1 = D
now recombine the nybbles to get 0x2D. HTH.
Of course, it could be pointed out that most systems have a calculator utility which will convert hex to binary, anyway. Oh, well.
Re:Representing binary values in C ?
You're right, ofcourse.Nick Sonneveld wrote: wouldn't that be
otherwise you'd just get zero?Code: Select all
BIT(1) | BIT(24) | BIT(15);
- Nick
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Representing binary values in C ?
maybe i'm completely of topic, but ... doesn't 0b10010110 do just what that guy wants ?
http://padmin1.ncsa.uiuc.edu/auxdocs/in ... cnst_c.htm
http://padmin1.ncsa.uiuc.edu/auxdocs/in ... cnst_c.htm
Re:Representing binary values in C ?
I don't believe that 0b syntax exists in standard C. I think your only options are octal, decimal, and hexadecimal.
Hexadecimal is relatively easy -- each hexadecimal digit represents four bits in the corresponding binary value.
0x8 corresponds to the binary value 1000
0x88 corresponds to the binary value 10001000
and so on.
Hexadecimal is relatively easy -- each hexadecimal digit represents four bits in the corresponding binary value.
0x8 corresponds to the binary value 1000
0x88 corresponds to the binary value 10001000
and so on.
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Representing binary values in C ?
i don't have my K&R here, but iirc, binary constants were ANSI ...
oops. Sorry. looks like they're not what they pretended to be8033 Binary constants are not permitted in ANSI C.