LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do you write binary literal constants in C? (https://www.linuxquestions.org/questions/programming-9/how-do-you-write-binary-literal-constants-in-c-135800/)

vasudevadas 01-17-2004 03:47 PM

How do you write binary literal constants in C?
 
In C, to write a hexadecimal literal constant, you prefix the figure with 0x, e.g. 0xffe0119f. To write an octal literal, you prefix it with 0, e.g. 0777. So how do you write a literal in binary?

kev82 01-17-2004 04:30 PM

ansi c does not support binary constants, you'll have to write your numbers in hex, octal or decimal

vasudevadas 01-17-2004 04:32 PM

What a bummer!

kev82 01-17-2004 05:09 PM

if its a real big problem then write something with sed/tr/perl that replaces 0b(binary number) with 0x(hex equivelent) in your source file and write a makefile that runs this before compiling the source. but as you can convert bin/hex bin/octal by inspection its not really worth it.

wapcaplet 01-17-2004 05:58 PM

Converting from binary to hex is really easy, though. Groups of four binary digits represent one hex digit. Probably the reason that binary literals aren't included; Real C Programmers know how to think in Hex anyway ;)

Check out this for more explanation.

vasudevadas 01-18-2004 07:39 AM

Quote:

Originally posted by wapcaplet
Converting from binary to hex is really easy, though. Groups of four binary digits represent one hex digit. Probably the reason that binary literals aren't included; Real C Programmers know how to think in Hex anyway ;)
I shall report for remedial spanking and beginners' introduction to number bases first thing in the morning, I promise. ;)

Has my licence to hack been revoked?


All times are GMT -5. The time now is 01:35 AM.