• Advertisement

• Popular Now

• 12
• 12
• 9
• 10
• 13
• Advertisement
• Advertisement
• Advertisement

Archived

This topic is now archived and is closed to further replies.

0x(whatever) for hex, what about binary?

This topic is 6063 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In C++, we use 0x and the the hex values to represents hexedecimal values like: x = 0xA; meaning the same as x = 10; How do I do that for binary values?

Share this post

Share on other sites
Advertisement
As far as I know, you can''t use binary directly. I use calc to convert from binary to hex when I have to.

Share this post

Share on other sites
Although converting binary to hex is so easy that you don''t really need a calculator. This is partly why hex is chosen as the standard representation for binary numbers.

Just break the binary number into blocks of 4 digits, and 4 binary digits (bits, obviously) is equivalent to 1 hexadecimal digits (which should probably be called hits ). The first bit is the ''1s'' column, the second is the ''2s'', the 3rd is the ''4s'' and the last column is the ''8s''. Add them up, and you have your hex digit, with A to F representing 10 to 15.

Example:
100101010011101 in binary100 1010 1001 1101 broken up (remember to start from the right)  4    A    9    C broken up in Hex0x4A9C in C code.

It''s that simple!

Share this post

Share on other sites
I think that one hex digit is called a "nibble". Not that it really matters.

Share this post

Share on other sites
Basically the answer to the original question is you can''t assign values directly as binary in C/C++ right?

Share this post

Share on other sites
100101010011101 is actually Ox489D

~prevail by daring to fail~

Share this post

Share on other sites
forget both Zerosignull and Kylotan because
100101010011101
is actually
0x4A9D

Share this post

Share on other sites
And if you can''t count to 15 (run out of toes), just go to 7 (base 8, or octal):

101 011 111 110
5 3 7 6

x = 05376; (leading zero indicates octal to C++).

Now isn''t that confusing?

You could also make a function to convert a "binary" string "101011111110" to an integer, involving a loop and some shifts.
However, last time I posted an answer to that, a mini mini flame war erupted

~~~
Cheers!
Brett Porter
PortaLib3D : A portable 3D game/demo libary for OpenGL

Share this post

Share on other sites
So i mis typed the dam thing ...

~prevail by daring to fail~

Share this post

Share on other sites
My actual answer was not what was important: I was teaching the methodology

Share this post

Share on other sites

• Advertisement