#### Archived

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

# Binary prefixes in C

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

## Recommended Posts

hi, does anyone know the binary prefix when it comes to defining one? For example, for Hexademical representation it is 0x... for Octal it is 0... how about binary?

##### Share on other sites
With binary, it''s a suffix , which is b . So 11010001b would mean that''s a binary number.

##### Share on other sites
Actually, while b is a valid suffix in assembly language, it''s not in C. There is no way to put in binary numbers in C/C++ code, as far as I know. But if there is, then I''d like to know too

##### Share on other sites
Some compilers may have provisions for binary constants, but the ANSI standard does not.

##### Share on other sites
And it''s really annoying that there isn''t a way. Hex is your closest bet.

-Mike

##### Share on other sites
about 8 years ago, I think I used %... to represent binary in a Borland compiler but I guess that doesn''t work now.

##### Share on other sites
Surely it's not too difficult to write a little function to let you use binary numbers?

Something like:

int Binary(char* binaryString);

Then you could do something like this:

int aNumber = Binary("01010111");

You could even write a macro do make it easier to use.

#define BINARY(n) Binary(#n)

int aNumber = BINARY(01010111)

And here's a sample function:

 ` int Binary(char* binaryString){ char* digit = binaryString+strlen(binaryString); int total = 0; int count = 0; do { total += ((*(--digit)-'0')<

Andy.

Edited by - andy maddison on February 2, 2001 4:01:14 PM

1. 1
2. 2
Rutin
22
3. 3
JoeJ
18
4. 4
5. 5

• 15
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631725
• Total Posts
3001905
×