Log in

View Full Version : binary in C


sciguy125
11-11-2005, 17:45
Does anyone know if it's possible to use binary numbers in C? Of course, it's just to make it easier to read the source. I'm doing some bitwise operations and 218 or 0xDA just isn't as meaningful as 11011010.

Dave Flowerday
11-11-2005, 18:28
Does anyone know if it's possible to use binary numbers in C? Of course, it's just to make it easier to read the source. I'm doing some bitwise operations and 218 or 0xDA just isn't as meaningful as 11011010.
I don't believe the C standards have any support for entering numbers in binary. Just use hex - it'll be awkward for a bit but after a while you'll be able to read hex and "see" the binary without any trouble. Binary becomes cumbersome when you start talking about 16 and 32 bit (and larger) numbers too.

Most people who write a lot of software will tell you that 0xDA is much easier to read than 11011010.

Mike Betts
11-11-2005, 19:41
I'm 1200 miles from my K&R right now...

0b11011010

should be equivalent to

0xDA

N'est-ce pas?

Dave Flowerday
11-11-2005, 19:58
I'm 1200 miles from my K&R right now...

0b11011010

should be equivalent to

0xDA
That's what I thought at first too, but gcc didn't seem to like it, and I found several discussions on some C newsgroups that suggested that binary constants aren't officially part of the language. However, I just checked it and MCC18 seemed to take it...

My advice still applies though: get used to hex, it's what everyone else uses ;)

Manoel
11-11-2005, 20:07
"The value of an integer can be specified in octal or hexadecimal instead of decimal. A leading 0 (zero) on an integer constant means octal; a leading 0x or 0X means hexadecimal".


So apparently you can't, unless the C18 compiler is different in that aspect (it isn't a 100% ANSI C, after all) - assuming you are asking this about the Microchip compiler.
I agree with Dave that the hexadecimal form is much better once you get used to it.
You probably know it, but for the ones that do not, each hex symbol represents 4 bits. Once you realize that, it just isn't magic anymore.

ghansel
11-11-2005, 20:11
0b should work. It's not ANSI C, but many compilers understand it. gcc compiles it fine. Dunno about C18 - I usually go out of my way to avoid binary.

http://refcards.com/refcards/c/c-refcard-letter.pdf

George

sciguy125
11-11-2005, 20:41
Well, I'm using SDCC (http://sdcc.sourceforge.net/). I'd use C18, but it doesn't work for the 16F series PICs. I just tried 0b and it didn't like it. I guess I'll have to stick to hex or decimal.

Matt Krass
12-11-2005, 01:09
The gcc port for Atmel AVRs supports it no questions asked, it's actually fairly used in the examples I saw so 0b works there.....

No support in SDCC? Darn I was gonna use it for this 16F I got sitting here. Oh well...

JJG13
13-11-2005, 15:39
I sometimes use a calculator to convert from binary to hex and then write the binary equivalent in a comment by the hexadecimal value. For example:
int bit2mask = 0x04; //00000100b

Alan Anderson
13-11-2005, 22:12
#define b00000000 0x00
#define b00000001 0x01
#define b00000010 0x02
#define b00000011 0x03
:
:
#define b11111110 0xFE
#define b11111111 0xFF

Generate 256 lines once, then let the preprocessor take care of it for you from then on.