473,669 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary constants

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:

010100001010111 011010111010110 10 ... (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
11 10169

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:

010100001010111 011010111010110 10 ... (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
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

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:

010100001010111 011010111010110 10 ... (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;
}
/*
000000000000000 000000000000000 000000000000000 000000000001111 1111
*/

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

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.
010100001010111 011010111010110 10 ... (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
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
red floyd <no*****@here.d udewrote:
>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
Steve Pope wrote:
red floyd <no*****@here.d udewrote:
>>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
red floyd <no*****@here.d udewrote:
>Steve Pope wrote:
>red floyd <no*****@here.d udewrote:
>>>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
Steve Pope wrote:
red floyd <no*****@here.d udewrote:
Steve Pope wrote:
red floyd <no*****@here.d udewrote:
>>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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
481
by: someone | last post by:
Suppose that I have a class in an assembly that is delivered to the user, what can I do to change the class so that it doesn't break the binary compatibility? That is, user application can run with recompiling and relinking. I know that if I define an interface, and only expose the interface but not the class which implments the interface, I can add a data member to the class without breaking the binary compatibility. If the class...
18
22598
by: Bern | last post by:
how to specifiy a binary number in c++? hex numbers are specified by 0x prefix
6
19853
by: Tom Torfs | last post by:
Hello All, I've been missing the lack of support for binary numeric literals in C. To get around it I wrote the following handy macros, which allows you to simply write something like: whatever = B8(10101010); and will translate as:
6
10546
by: Kelvin | last post by:
Hi everyone: when we wanna use hex numbers in C, we usually write something like: int hex_num = 0x12F9; but how can I declare a binary number in a similar way by putting some leading words to tell the complier this is a binary number??? similarly, in printf, we have %d for an decimal number %x and %o for hex and octal numbers... how about binary numbers?? Thank you very much...
3
14403
by: Stephen Mayes | last post by:
Is there any way to represent a literal numerical value in binary? My wish would be something like 0b0011 instead of the hex 0x0F. ( if this is even the same. ) Sometimes I find it's easier to use my finger's than my brain.
5
6735
by: serrand | last post by:
Hello all, is there a way in order to write binary numbers as hexa or octal in c ? Xavier
34
3365
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const long double const_pi=0.0; lines to // rodata
19
18826
by: sethukr | last post by:
Hi everybody, Can we assign binary values to an integer variable like assigning Hexadecimal values??? Thanks, Sethu
7
11370
by: Gary Brown | last post by:
Hi, I have a whole bunch of integer constants that are best given in octal (PDP-1 opcodes). It makes a huge difference in readability and authenticity if these can be entered in octal. I haven't found a way to do this. Is there one? Thanks, Gary
0
8383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.