|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
binary in C
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.
|
|
#2
|
||||
|
||||
|
Re: binary in C
Quote:
Most people who write a lot of software will tell you that 0xDA is much easier to read than 11011010. |
|
#3
|
||||
|
||||
|
Re: binary in C
I'm 1200 miles from my K&R right now...
0b11011010 should be equivalent to 0xDA N'est-ce pas? |
|
#4
|
||||
|
||||
|
Re: binary in C
Quote:
My advice still applies though: get used to hex, it's what everyone else uses ![]() |
|
#5
|
|||||
|
|||||
|
Re: binary in C
Quote:
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. |
|
#6
|
|||
|
|||
|
Re: binary in C
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 |
|
#8
|
||||
|
||||
|
Re: binary in C
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... |
|
#9
|
||||
|
||||
|
Re: binary in C
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:
Code:
int bit2mask = 0x04; //00000100b |
|
#10
|
|||||
|
|||||
|
Re: binary in C
Code:
#define b00000000 0x00 #define b00000001 0x01 #define b00000010 0x02 #define b00000011 0x03 : : #define b11111110 0xFE #define b11111111 0xFF |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Binary in Rose is Rose | MisterX | Chit-Chat | 8 | 11-02-2004 18:26 |
| pic: Dawning of a New Age. | CD47-Bot | Extra Discussion | 90 | 31-07-2003 22:04 |
| Fun with sensors | Team852 | Technical Discussion | 7 | 06-01-2003 00:07 |
| A couple of noodle scratchers | Cheese Head | Programming | 11 | 07-12-2002 09:59 |
| delphi: binary sort | archiver | 2000 | 0 | 24-06-2002 00:30 |