Binary (base 2) in C

Status
Not open for further replies.

Blaze

Seniorius Lurkius
20
I am working on an embedded system project, a robot based on Motorola 68HC12 microcontroller. I'm using the ImageCraft C compiler. I am often writing to various registers. Right now I am representing all control registers in HEX, PWCLK = 0x50. It would be much clearer code if I could represent all control registers in binary, PWCLK = 01010000. I know that there is got to be simple way, I just can't find it.


geoff
 

gaffa

Well-known member
6,210
I haven't got any manuals with me to verify this, AFAIR it's %b, e.g.
%b 01010000, or %o 120 for octal. I was brought up on octal/binary, and still find them easier to use than HEX. May be it's just the subset, superset thing.
The most frustrating bit though, is that C doesn't have good support for bit datatypes. You can't (or couldn't) have arrays of bits like you can in say, Pascal.

Cheers

Gaffa.
 

Blaze

Seniorius Lurkius
20
Thanks for the replies.

Ear- I think that in this case binary would be clearer. It is not the value of register as a hole; it is the value bits in register that are important. i.e. bit0 controls this, bit1 controls that, etc'¦ Everything can be done in HEX, but is just one more layer of abstraction.

Gaffa- I tried %b, but it doesn't work. The compiler complains about unknown identifier.

Anybody have any more ideas?


geoff
 

wb

Ars Legatus Legionis
25,208
Subscriptor
For embedded systems, I usually do this two different ways depending upon how many different registers I need to play with, and how often I access them in the code. I've done some commonly used register settings before with preprocessor stuff. e.g.:

#define TOGGLE_S (reg_addr ^ 0x40)


The other way I've done this kind of stuff in the past is write functions to enable and disable bits in registers, and I use preprocessor definitions for each register bit. e.g.:


#define S_BIT 0
#define V_BIT 1
#define Z_BIT 2
.
.
.

setRegBits( S_BIT | V_BIT );
 

DrStrange

Wise, Aged Ars Veteran
156
The way I've always handled writing to registers is to keep a mask in memory, usually an integer, and then AND or OR values into that mask and write the entire mask to the register. That way even if you can't read from the regsiter you have the contents of the register in memory and you won't overwrite anything important. And to answer the OP, hex is the only way I've ever seen it done in C or C++.
 

earl

Well-known member
3,066
We do some embedded programming. A fellow coworker (read moron) wrote a complicated macro to allow him to use inline binary. Try figuring out exactly which bit is on in a 32b value and you will quickly see why I have a strong preference for hex. However, to each his own. If you want, you can certainly write your own macro. It won't look quite as natural as built in support, but it will allow you to figure out for yourself why binary is bad ;) I guess I just wrote enough code that I can glance at hex and see the binary.

-earl-
 

gaffa

Well-known member
6,210
I found this on the net:

Integers constants may be defined in decimal integer format (e.g., 4053 or -1), hexadecimal format using the "0x" prefix (e.g., 0x1fff), and a non-standard but useful binary format using the "0b" prefix (e.g., 0b1001001). Octal constants using the zero prefix are not supported.

interactive C

Hopefully this non-standard way will work for you. It obviously works here, and I sure it works with DEC (ANSI) C. It could also be a DEC extension, but how difficult is it to add this to the compiler or pre-processor?

Cheers

Gaffa.
 

geremy

Ars Scholae Palatinae
1,065
The other thing you could do is if you are really really concerned with readability is bit packed structure. TI does this all the time in their DSP code. So define the structure, declare it as a structure pointer, and set it equal to the address of the register. Not exactly the most efficient way of doing things but it's very readable and it works (for me at least).

code:<HR>
Code:
#define REGISTER_ADDRESS 0x100000<P>struct some_reg_struct{
    unsigned int some_bit_1:1;
    unsigned int some_bit_2:1;
    unsigned int some_bit_3:1;
    unsigned int some_field_1:13;
}<P>struct some_reg_struct * some_reg = (struct some_reg_struct *)REGISTER_ADDRESS;<P>/* you can then access the bits directly */
some_reg->some_bit_2 = 0;

-typedefs removed-

Hope this helps
 

HL7

Ars Praefectus
3,523
Subscriptor++
You could always #define binary constants, I think I've done this before ....

code:<HR>
Code:
#define b00000000 0x00
#define b00000001 0x01
#define b00000010 0x02
...
#define b11111111 0xFF

Rather than type them all in, you could even write a program to generate these #define statements to put into your program. I even did it for you if you want it.

code:<HR>
Code:
#include <stdio.h><P>int pow(b, e)
{
	int i;
	int r = 1;<P>	for (i = 0; i < e; i++) {
		r *= b;
	}<P>	return r;
}<P>int main(void)
{
	int i, j;<P>	for (i = 0; i < 256; i++) {
		printf ("#define b");
		for (j = 7; j >= 0; j--) {
			printf ("%c", '0' + (i & pow(2, j)) / pow(2, j), pow(2,j));
		}
		printf (" %x\n", i);
	}<P>	return 0;
}
 
I also have to program embedded DSP.
In the beginning, I also use binary representation. However, after a while, I find hexadecimal to be a more "compact" and cleaner way to represent binary digits.

single digit hex number can represent 4 digits binary number.
0x0 = 0000
0x1 = 0001
0x2 = 0010
0x3 = 0011
...
0xD = 1101
0xE = 1110
0xF = 1111


2 digit hex number can represent 8 digits binary number (a char type)

So, if I want to represent 10000000_binary, I simply do 0x80
instead of type 8 digits, I typed 4 (including 0x)

Believe me, if you are going to mess with lots of binary values,
hexadecimal will be your friend.

Just spend some time to learning to do direct binary to hex translation in your head, and you will find yourself saving lots of time(and typing).

Also, when you communicate with fellow engineer, they prefer hex than binary.

Engineer A: "Hey, you forget to set the 4th digit of the register X, the register content should be four-ones-twentyfour-zero's-one-zero-zero-zero
(1111 0000 0000 0000 0000 0000 0000 1000)"

or

Engineer B: "Hey, you forget to set the 4th digit of the register X,
the register content should be (hex) F0000008"

It is worth it to learn hex number.
 
Status
Not open for further replies.