Archived

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

Xtreme

Binary prefixes in C

Recommended Posts

Xtreme    122
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 this post


Link to post
Share on other sites
Qoy    127
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
And it''s really annoying that there isn''t a way. Hex is your closest bet.

-Mike

Share this post


Link to post
Share on other sites
andy maddison    122
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')<<count++);
}
while(digit != binaryString);

return total;
}


Andy.

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

Share this post


Link to post
Share on other sites