Binary prefixes in C
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?
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.
about 8 years ago, I think I used %... to represent binary in a Borland compiler but I guess that doesn''t work now.
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:
Andy.
Edited by - andy maddison on February 2, 2001 4:01:14 PM
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
Popular Topics
Advertisement