Sign in to follow this  
Simplicity

Specifing binary in C++

Recommended Posts

Ra    1062
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.

Share this post


Link to post
Share on other sites
rip-off    10979
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
iMalc    2466
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
iMalc    2466
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this