We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
How can a Binary number be used in a program?
There are only 10 ways of using a binary number -:)
what are those?
Define "program". Define "use".
C code ? Use hex or octal instead. C does not support binary constants.
Assembly code ? Read the manual concerning "expressions". That will explain what format is used for binary datatypes.
If you are asking about using binary constants, here are some public domain binary constant macros Tom Torfs published on Usenet. I'm a little concerned because last time I posted this here, the preview was correct, but the forum rendering was not -- here goes:
================================================================ From: tomtorfs@village.uunet.be (Tom Torfs) Newsgroups: comp.lang.c,comp.arch.embedded Subject: Binary constant macros Date: 26 Feb 2004 07:36:35 -0800 Hello All, I've been missing the lack of support for binary numeric literals in C. To get around it I wrote the following handy macros, which allows you to simply write something like: whatever = B8(10101010); and will translate as: whatever = 85; (compile-time constant) Code below... hopefully it's useful to some of you as well. greetings, Tom /* Binary constant generator macro By Tom Torfs - donated to the public domain */ /* All macro's evaluate to compile-time constants */ /* *** helper macros *** / /* turn a numeric literal into a hex constant (avoids problems with leading zeroes) 8-bit constants max value 0x11111111, always fits in unsigned long */ #define HEX__(n) 0x##n##LU /* 8-bit conversion function */ #define B8__(x) ((x&0x0000000FLU)?1:0) \ +((x&0x000000F0LU)?2:0) \ +((x&0x00000F00LU)?4:0) \ +((x&0x0000F000LU)?8:0) \ +((x&0x000F0000LU)?16:0) \ +((x&0x00F00000LU)?32:0) \ +((x&0x0F000000LU)?64:0) \ +((x&0xF0000000LU)?128:0) /* *** user macros *** / /* for upto 8-bit binary constants */ #define B8(d) ((unsigned char)B8__(HEX__(d))) /* for upto 16-bit binary constants, MSB first */ #define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) \ + B8(dlsb)) /* for upto 32-bit binary constants, MSB first */ #define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24) \ + ((unsigned long)B8(db2)<<16) \ + ((unsigned long)B8(db3)<<8) \ + B8(dlsb)) /* Sample usage: B8(01010101) = 85 B16(10101010,01010101) = 43605 B32(10000000,11111111,10101010,01010101) = 2164238933 */ greetings, Tom
If this is true...
whatever = B8(10101010); and will translate as: whatever = 85;
I'm not sure I want to use this code.
10101010 in Binary = 0xAA in Hexadecimal.
Jon
Looks like Mr. Torfs has a typo. Further down just above the second of his two(!) signatures in "Sample usage:", he's got the same "... = 85" but this time it's B8(01010101), which is correct.
he's got the same "... = 85" but this time it's B8(01010101), which is correct.
Hmmm. I always thought 01010101 was 0x55. :-)
"Looks like Mr. Torfs has a typo."
I should have added that the typo is only in his commentary. As far as I know, the macros themselves are fine to use as-is (as-are?).
"I always thought 01010101 was 0x55."
You're closer than I was. Taking 01010101 by itself, I came up with 266305.
if You absolutotally HAVE to do it
#define bin00000001 0x01 ... define bin11111111 0xff
I do some 'binary' definitions this way
in my .h file
sfr S0_TMR2CN = 0xc8; // TIMER 2 CONTROL */ sfr S1_TMR3CN = 0xc8; // TIMER 3 CONTROL sfr S2_TMR4CN = 0xc8; // TIMER 4 CONTROL sbit SB0_TMR2CN_TF = 0xcf; sbit SB1_TMR3CN_TF = 0xcf; sbit SB1_TMR3CN_TR = 0xca; #define SM_TMRxCN_TF 0x80 // oflo/int #define SM_TMRxCN_EXF 0x40 // ext activate #define SM_TMRxCN_EXEN 0x08 // ext stimuli #define SM_TMRxCN_TR 0x04 // run control #define SM_TMRxCN_C_T 0x02 // 1 = counter 0=timer #define SM_TMRxCN_CP_RL 0x01 // 0 = reload, 1 = capture
then in the .c file
//S0_TMR2CN = 0 + SM_TMRxCN_TF + SM_TMRxCN_EXF + SM_TMRxCN_EXEN + SM_TMRxCN_TR + SM_TMRxCN_C_T + SM_TMRxCN_CP_RL; S2_TMR4CN = 0 + SM_TMRxCN_TR + SM_TMRxCN_CP_RL;