LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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
  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: 51
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: 51
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
LQ 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



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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:58 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration