Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org How do you write binary literal constants in C?
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 01-17-2004, 03:47 PM #1 vasudevadas Member   Registered: Jul 2003 Location: Bedford, UK Distribution: Slackware 11.0, LFS 6.1 Posts: 519 Rep: 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?
 01-17-2004, 04:30 PM #2 kev82 Senior Member   Registered: Apr 2003 Location: Lancaster, England Distribution: Debian Etch, OS X 10.4 Posts: 1,263 Rep: ansi c does not support binary constants, you'll have to write your numbers in hex, octal or decimal
 01-17-2004, 04:32 PM #3 vasudevadas Member   Registered: Jul 2003 Location: Bedford, UK Distribution: Slackware 11.0, LFS 6.1 Posts: 519 Original Poster Rep: What a bummer!
 01-17-2004, 05:09 PM #4 kev82 Senior Member   Registered: Apr 2003 Location: Lancaster, England Distribution: Debian Etch, OS X 10.4 Posts: 1,263 Rep: 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.
 01-17-2004, 05:58 PM #5 wapcaplet LQ Guru   Registered: Feb 2003 Location: Colorado Springs, CO Distribution: Gentoo Posts: 2,018 Rep: 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. Last edited by wapcaplet; 01-17-2004 at 06:00 PM.
01-18-2004, 07:39 AM   #6
Member

Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Original Poster
Rep:
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?

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post bidouilleur Linux - Software 5 10-09-2010 09:23 PM xcmore Programming 11 06-17-2005 07:48 AM master Programming 4 09-05-2004 04:36 PM BrianK Programming 3 06-22-2004 02:11 PM dushkinup Programming 1 05-02-2004 01:08 PM

All times are GMT -5. The time now is 09:45 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -