LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 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: Reputation: 30
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?
 
Old 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: Reputation: 50
ansi c does not support binary constants, you'll have to write your numbers in hex, octal or decimal
 
Old 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: Reputation: 30
What a bummer!
 
Old 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: Reputation: 50
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.
 
Old 01-17-2004, 05:58 PM   #5
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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.
 
Old 01-18-2004, 07:39 AM   #6
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grip : no write access to write encoded file bidouilleur Linux - Software 5 10-09-2010 09:23 PM
read & write binary file error in redhat xcmore Programming 11 06-17-2005 07:48 AM
python cant assign to literal master Programming 4 09-05-2004 04:36 PM
How do you set a shell script var with a literal '*'? BrianK Programming 3 06-22-2004 02:11 PM
CSS - Constants? dushkinup Programming 1 05-02-2004 01:08 PM


All times are GMT -5. The time now is 08:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration