Advertisement

Binary prefixes in C

Started by January 31, 2001 05:55 PM
5 comments, last by Xtreme 24 years ago
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?
Yes! There are kangaroos in Australia but I haven't seen them...yet
With binary, it''s a suffix , which is b . So 11010001b would mean that''s a binary number.
Advertisement
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
Some compilers may have provisions for binary constants, but the ANSI standard does not.
And it''s really annoying that there isn''t a way. Hex is your closest bet.

-Mike
about 8 years ago, I think I used %... to represent binary in a Borland compiler but I guess that doesn''t work now.
Yes! There are kangaroos in Australia but I haven't seen them...yet
Advertisement
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

This topic is closed to new replies.

Advertisement