Page 1 of 1

Representing binary values in C ?

Posted: Sat Sep 27, 2003 8:00 am
by Perica
..

Re:Representing binary values in C ?

Posted: Sat Sep 27, 2003 9:52 am
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

Re:Representing binary values in C ?

Posted: Sat Sep 27, 2003 9:52 pm
by Perica
..

Re:Representing binary values in C ?

Posted: Sun Sep 28, 2003 5:33 am
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;


Re:Representing binary values in C ?

Posted: Sun Sep 28, 2003 6:26 am
by sonneveld
wouldn't that be

Code: Select all

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

- Nick

Re:Representing binary values in C ?

Posted: Sun Sep 28, 2003 11:28 am
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.

Re:Representing binary values in C ?

Posted: Mon Sep 29, 2003 4:21 am
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.

Re:Representing binary values in C ?

Posted: Fri Oct 03, 2003 4:47 am
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

Re:Representing binary values in C ?

Posted: Fri Oct 03, 2003 5:34 am
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.

Re:Representing binary values in C ?

Posted: Fri Oct 03, 2003 6:55 am
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 :(