Specifing binary in C++

Started by
6 comments, last by iMalc 18 years, 6 months ago
How to specify in binary in C++ just like using the 0x to specify hex. In addtion, how to read single bits from a data item, say from an long or a file?
Advertisement
You can't specify binary constants in C++.

As for reading single bits from data, you'll have to mask off the bits you don't want (using bitwise AND) and test them individually. It's not pretty.
Ra
Quote:Original post by Simplicity
How to specify in binary in C++ just like using the 0x to specify hex. In addtion, how to read single bits from a data item, say from an long or a file?


hex and binary are easily related.

0x1 = 0001
0x2 = 0010
0x3 = 0011
0x4 = 0100

...

so if you want a binary constant in your program 00001010

it 0x4 + 0x2 = 0x6

the real question is what are you doing that requires reading indidual bits to be read or written.
Boost has a "Binary int" template in review, but it is unclear wether it will be accepted. It should be available in the file vault on http://boost.org/.
Actually, I also have a binary macro up for review at boost (simultaneously with scott schurr's template version, but mine's of course way cooler and more functional than the template version :p).

You can download it here

Use it like:

BOOST_BINARY_LITERAL( 1010 10 11 01110 010 )

Bit groupings can be from 1 to 8 bits in size, it works with any size integers your compiler can handle. If you want to use standard integer suffixes, use:

BOOST_SUFFIXED_BINARY_LITERAL( 10010 10 0110, UL )

The names are long for clarity, but they may be shortened.

[Edited by - Polymorphic OOP on October 23, 2005 12:55:41 PM]
Impressive Polymorphic OOP!
I can certainly see why it stops at allowing at most 8 bit groupings! Though it would help enforce good style anyway, as it looks better with appropriate whitespace. Obviously that header file is largely generated by some other code.

rip-off: You might want to correct your wrong example which should equal 0xA, not 0x6, or change it to 00000110.

Seriously though, I think every programmer should just learn hexadecimal and the conversion to/from binary. Write your constant in hex, with the equivalent binary in a comment if you like.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
Impressive Polymorphic OOP!
I can certainly see why it stops at allowing at most 8 bit groupings! Though it would help enforce good style anyway, as it looks better with appropriate whitespace. Obviously that header file is largely generated by some other code.

Thanks. Yeah, the header would get massive if I went much beyond 8 bits max per grouping.

Quote:Original post by iMalc
Seriously though, I think every programmer should just learn hexadecimal and the conversion to/from binary. Write your constant in hex, with the equivalent binary in a comment if you like.

Right, it's good to know both, as well as octal (my macro converts the binary to octal internally), but anytime you are working with bitwise stuff, such as masking, etc. it's more clear to a reader if the mask is shown in bits rather than with hex, without the need for an extra comment.
Yeah I saw something about octal in there, which surprised me a bit. I guess it's less work grouping 3 bits at a time than grouping 4 bits at a time, or something like that.

It would have been nice if in C++ you could simply write 0b01100101 (with a b rather than an x, for binary). Of course that would also compile a lot quicker too.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement