Representing binary values in C ?

Programming, for all ages and all languages.
Post Reply
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Representing binary values in C ?

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 9:18 pm, edited 1 time in total.
Jamethiel

Re:Representing binary values in C ?

Post by Jamethiel »

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
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:Representing binary values in C ?

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 9:19 pm, edited 1 time in total.
mystran

Re:Representing binary values in C ?

Post by mystran »

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.

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;

sonneveld

Re:Representing binary values in C ?

Post by sonneveld »

wouldn't that be

Code: Select all

BIT(1) | BIT(24) | BIT(15);
otherwise you'd just get zero?

- Nick
Schol-R-LEA

Re:Representing binary values in C ?

Post by Schol-R-LEA »

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.
mystran

Re:Representing binary values in C ?

Post by mystran »

Nick Sonneveld wrote: wouldn't that be

Code: Select all

BIT(1) | BIT(24) | BIT(15);
otherwise you'd just get zero?

- Nick
You're right, ofcourse.
User avatar
Pype.Clicker
Member
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 ?

Post by Pype.Clicker »

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
ark

Re:Representing binary values in C ?

Post by ark »

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.
User avatar
Pype.Clicker
Member
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 ?

Post by Pype.Clicker »

i don't have my K&R here, but iirc, binary constants were ANSI ...
8033   Binary constants are not permitted in ANSI C.
oops. Sorry. looks like they're not what they pretended to be :(
Post Reply