Jump to content

How to represent binary number in C


Recommended Posts

I am trying to write a C program where I need to work directly with some binary numbers. The only thing that I can not find, and yes I have looked, is how to represent binary numbers.

 

Basically what I am looking for is how to store am a 8 bit binary number, say 0110110, in a variable. The best that I have found so far would be to do something like this:

 

#define BYTE char

BYTE myBinaryNo = 0x0110110;

 

Would this be correct though? I thought the 0x notation was for hex numbers.

Link to comment
Share on other sites

I did a little bit of this last year AFAIK you represent binary numbers (bit patterns) with Hexadecimal numbers. My notes actually say "Most computers use hexadecimal numbers to represent bit patterns, though some use octal numbers for this purpose."

 

Binary Hexadecimal

0000 0

0001 1

0010 2

0011 3

0100 4

0101 5

0110 6

0111 7

1000 8

1001 9

1010 A

1011 B

1100 C

1101 D

1110 E

1111 F

 

I'm sure you can convert between bases and have seen a table like this before, but it was pretty looking and easy to copy and paste in :P

 

I remember it as a leading 0 for octal and leadin 0x for hex. (could be wrong). I haven't come across rolands method before.

Edited by willisoften
Link to comment
Share on other sites

what ? I've just tried with my favourit compiler it compiles !

I check the spec in case its not 100% compliant but on that matter I would be surprised. A moment please ....

can't find my old book. I've asked on an other forum.

On my old Symantec C++ 6.0 book (now Digital Marc C++) it says the prefix is 0b.

On my even older Borland C++ 3.0 book they don't say anything about binary constant definition. I'll let you know.

what's your compiler ?

 

roland

Link to comment
Share on other sites

can't find my old book. I've asked on an other forum.

On my old Symantec C++ 6.0 book (now Digital Marc C++) it says the prefix is 0b.

On my even older Borland C++ 3.0 book they don't say anything about binary constant definition. I'll let you know.

what's your compiler ?

My compiler is gcc 3.2.something. Although part of the problem here might be that you are using C++ and I am using C. There are many differences between the two.

Link to comment
Share on other sites

Ok I had the answer: 0b prefix is my compiler specific, sorry..

 

It's DMC specific. As far as I know, no other compiler does this.

 

"roland" <--rv@ronetech.com> wrote in message

news:bobd7c$4i8$1@digitaldaemon.com...

 

>> Hi,

>>

>> With DM the prefix for a binary integer constant is 0b:

>>

>> unsigned char aBinaryValue = 0b11010;

>>

>> My question is: is this the standard or is it DM specific ?

>>

>> Thanks

>>

>> roland

>>

>> PS1: I can't find my old Kerighan & Ritchie book,

>> PS2: It's to help somebody. No there is little chance I switch to an

>> other compiler until I die except if I completely switch to Linux...

>>

 

 

roland

Link to comment
Share on other sites

I am trying to write a C program where I need to work directly with some binary numbers. The only thing that I can not find, and yes I have looked, is how to represent binary numbers.

 

Basically what I am looking for is how to store am a 8 bit binary number, say 0110110, in a variable. The best that I have found so far would be to do something like this:

 

<!--QuoteEBegin-->#define BYTE char<!--QuoteEBegin--><!--QuoteEBegin-->BYTE myBinaryNo = 0x0110110;<!--QuoteEBegin-->

 

Would this be correct though? I thought the 0x notation was for hex numbers.

I think you are getting mixed up with notation vs actual storage. All data is stored in binary. It's just how the rules that are applied based on type. Assumming you are working with a char or byte (8 bit) type

 

BYTE myBinaryNo = 0xFF

 

would be equivalent to 1111 1111 in binary notation.

 

But if your interested in typing c code to represent binary notation, then 0b11111111 might work if your compiler believes in it (LOL). Probably more simple to use hex notation as its universal.

Link to comment
Share on other sites

sometime binary expression is more explicit than hex expression. for example for bit mask.

if 0b prefix does not work :oops: a notation can be:

 

unsigned abitmask = (1<<4) | (1<<7);

 

for 10010000

 

or

 

unsigned abitmask = ~((1<<4) | (1<<7));

 

for 01101111

 

(of course those expressions are evaluated at compile time)

 

roland

Link to comment
Share on other sites

  • 5 years later...
Guest whoever

even though this topic seems to have ended... wouldn't it have been easier to just use

 

bool [8]. and if needed transfer it to a int using the representation table above?

Link to comment
Share on other sites

  • 2 years later...
Guest rchandelier

A few compilers (usually Microcontroller's ones) has a special feature implemented within recognizing literal binary numbers by prefix "0b..." preceding the number, although most compilers (C/C++ standards) don't have such feature and if it is the case, here it is my alternative solution:

#define B_0001	1
#define B_0010	2
#define B_0011	3
#define B_0100	4
#define B_0101	5
#define B_0110	6
#define B_0111	7
#define B_1000	8
#define B_1001	9
#define B_1010	a
#define B_1011	b
#define B_1100	c
#define B_1101	d
#define B_1110	e
#define B_1111	f

#define _B2H(bits)	B_##bits
#define B2H(bits)	_B2H(bits)
#define _HEX(n)		0x##n##UL
#define HEX(n)		_HEX(n)
#define _CCAT(a,B)	a##b
#define CCAT(a,B)   	_CCAT(a,B)

#define BYTE(a,B)			HEX( CCAT(B2H( a),B2H( B)) )
#define WORD(a,b,c,d)			HEX( CCAT(CCAT(B2H( a),B2H( B)),CCAT(B2H( c),B2H( d))) )
#define DWORD(a,b,c,d,e,f,g,h)	HEX(  CCAT( CCAT(CCAT(B2H( a),B2H( B)),CCAT(B2H( c),B2H( d))),CCAT(CCAT(B2H( e),B2H( f)),CCAT(B2H( g),B2H( h))) )  )

//using example
char b = BYTE(0100,0001); //equivalent to b = 65; or b = 'A'; or b = 0x41;
unsigned int w = WORD(1101,1111,0100,0011); //equivalent to w = 57155; or w = 0xdf43;
unsigned long int dw = DWORD(1101,1111,0100,0011,1111,1101,0010,1000); //equivalent to dw = 3745774888; or dw = 0xdf43fd28;

 

Disadvantages: (it's not such a big ones)

- The binary numbers have to be grouped 4 by 4;

- The binary literals have to be only unsigned integer numbers;

Advantages:

- Total preprocessor driven, not spending processor time in pointless operations (like "?.. :..", "<<", "+") to the executable program (it may be performed hundred of times in the final application);

- It works "mainly in C" compilers and C++ as well (template+enum solution works only in C++ compilers);

- It has only the limitation of "longness" for expressing "literal constant" values. There would have been earlyish longness limitation (usually 8bits:0-255) if one had expressed constant values by parsing resolve of "enum solution"(usually 255 = reach enum definition limit), differently, "literal constant" limitations, in the compiler allows greater numbers;

- Some other solutions demand exagerated number of constant definitions (#define's in my opinion) including long or several header files (in most cases not easily readable and understandable, and make the project become unnecessarily confused and extended, like that using "BOOST_BINARY()");

- Simplicity of the solution: easily readable, understandable and adjustable for other cases (could be extended for grouping 8 by 8 too);

 

I hope it helps, thanks. Renato Chandelier

Edited by rchandelier
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...