By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 720 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

Binary constants

P: n/a
Hi,
what is the best way to hardcode into a C/C++ program binary
values/constants/etc. Since:

int a;
a=0b0111;

is wrong, how should I do it. I thought about converting them first to
hexadecimal or decimal, but what if you have a long string such as:

01010000101011101101011101011010 ... (64 digits long, for example for
__int64)

is there a way to handle this? And yes I have seen somewhere a very
smart macro that lets 0b01010 ... but it is limited to short strings.
Thanks,
Valerij

Dec 9 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a

valerij napsal:
Hi,
what is the best way to hardcode into a C/C++ program binary
values/constants/etc. Since:

int a;
a=0b0111;

is wrong, how should I do it. I thought about converting them first to
hexadecimal or decimal, but what if you have a long string such as:

01010000101011101101011101011010 ... (64 digits long, for example for
__int64)

is there a way to handle this? And yes I have seen somewhere a very
smart macro that lets 0b01010 ... but it is limited to short strings.
Thanks,
Valerij
I (personally) would use hexadecimal mode. I do not think there may be
any "nice" solution, which doesn't call external function. 16 (+ 2)
characters for 64-bit number is not too much, I think it is no problem
(it is better than 64-bit binary representation).

Dec 9 '06 #2

P: n/a
valerij napsal:
Hi,
what is the best way to hardcode into a C/C++ program binary
values/constants/etc. Since:

int a;
a=0b0111;

is wrong, how should I do it.
That's because any constant starting with 0 is octal. (It
may interest people to know that in a simple expression like
i = 0, the "0" is octal and not decimal. Whereas in i = 1,
the "1" is decimal and not octal.)

You have your choice of octal, decimal, and hexadecimal.
Probably most systems programmers use hexadecimal. I agree
there's ocassions where binary would be useful, which is why
it is an option in some other languages.

Steve
Dec 9 '06 #3

P: n/a

valerij wrote:
Hi,
what is the best way to hardcode into a C/C++ program binary
values/constants/etc. Since:

int a;
a=0b0111;

is wrong, how should I do it. I thought about converting them first to
hexadecimal or decimal, but what if you have a long string such as:

01010000101011101101011101011010 ... (64 digits long, for example for
__int64)

is there a way to handle this? And yes I have seen somewhere a very
smart macro that lets 0b01010 ... but it is limited to short strings.
Thanks,
Valerij
Take a look at std::bitset:
http://www.sgi.com/tech/stl/bitset.html

#include <iostream>
#include <ostream>
#include <bitset>

int main() {
std::bitset< 64 bset(0xff);
std::cout << bset << std::endl;
}
/*
00000000000000000000000000000000000000000000000000 00000011111111
*/

Note: a std::bitset< 64 is *not* the same type as std::bitset< 32 >
or any other size except 64.
Its not a true STL container but quite powerful in that it can easily
be converted to a string and has useful operators available. Its
input/output streameable too.

Dec 9 '06 #4

P: n/a

valerij wrote:
Hi,
what is the best way to hardcode into a C/C++ program binary
values/constants/etc. Since:
in C++ integer literals can only be in decimal, octal and hex.
01010000101011101101011101011010 ... (64 digits long, for example for
__int64)
In the C++ standard it says if an integer literal value can not be
stored in an int or long int than its undefined. So on most 32 bits
that would make a 64 bit integer literal illegal.
is there a way to handle this?
how about using string literals, and when your program starts up,
convert the binary string literals into __int64 at runtime?

-
Ivan
http://www.0x4849.net

Dec 9 '06 #5

P: n/a
Steve Pope wrote:
valerij napsal:
>Hi,
what is the best way to hardcode into a C/C++ program binary
values/constants/etc. Since:

int a;
a=0b0111;

is wrong, how should I do it.

That's because any constant starting with 0 is octal.
0x....
Dec 10 '06 #6

P: n/a
red floyd <no*****@here.dudewrote:
>That's because any constant starting with 0 is octal.
>0x....
No, 0x is hexadecimal, 0 without the x is octal.

Steve
Dec 10 '06 #7

P: n/a
Steve Pope wrote:
red floyd <no*****@here.dudewrote:
>>That's because any constant starting with 0 is octal.
>0x....

No, 0x is hexadecimal, 0 without the x is octal.

Steve
I'm aware of that. However, to be precise, any octal constant starts
with '0'.
Dec 11 '06 #8

P: n/a
red floyd <no*****@here.dudewrote:
>Steve Pope wrote:
>red floyd <no*****@here.dudewrote:
>>>That's because any constant starting with 0 is octal.
>>0x....

No, 0x is hexadecimal, 0 without the x is octal.
>Steve
>I'm aware of that. However, to be precise, any octal constant starts
with '0'.
Right, my mistake.

Any constant starting with 0 is either hexadecimal (if it starts
with 0x) or octal (otherwise).

Steve

Dec 11 '06 #9

P: n/a
Steve Pope wrote:
red floyd <no*****@here.dudewrote:
Steve Pope wrote:
red floyd <no*****@here.dudewrote:
>>That's because any constant starting with 0 is octal.
>0x....

No, 0x is hexadecimal, 0 without the x is octal.
Steve
I'm aware of that. However, to be precise, any octal constant starts
with '0'.

Right, my mistake.

Any constant starting with 0 is either hexadecimal (if it starts
with 0x) or octal (otherwise).
0.1 is decimal. This is all not really relevant though: the original
code used a constant starting with 0b which is not allowed in standard
C++, so even if a compiler happens to accept it (as an extension), the
standard rules do not apply.

Dec 11 '06 #10

P: n/a
red floyd wrote:
Steve Pope wrote:
>red floyd <no*****@here.dudewrote:
>>>That's because any constant starting with 0 is octal.
0x....
No, 0x is hexadecimal, 0 without the x is octal.

Steve

I'm aware of that. However, to be precise, any octal constant starts
with '0'.
Sorry, meant Hex.
Dec 11 '06 #11

P: n/a
Harald van Dijk <tr*****@gmail.comwrote:
>Steve Pope wrote:
>Any constant starting with 0 is either hexadecimal (if it starts
with 0x) or octal (otherwise).

0.1 is decimal. This is all not really relevant though: the original
code used a constant starting with 0b which is not allowed in standard
C++, so even if a compiler happens to accept it (as an extension), the
standard rules do not apply.
Wokay. Any INTEGER constant starting with 0 is either hexadecimal
(if it starts with 0x) or octal (otherwise).

I know we were talking integers at the start of this thread...

Steve
Dec 11 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.