bool bit dumb

Started by
2 comments, last by markdezonia 20 years, 3 months ago
struct bool8 { bool b[8]; }; bool8 b8; b8.b[0]=1; b8.b[1]=1; bool * b1=&b8.b[0]; bool * b2=&b8.b[1]; printf ("%d\n",b1); printf ("%d\n",b2); they don''t share the same address? you can''t use a pointer to point to a bit only to a char, 8bits. does this mean int + bool really means int + char? int i=0; bool b=1; i=i+b; Does this do ? Adds 1*(8bits)char to this 4*(8bit) int? how do you add (4bits)of data to (8bits) of data?, if the smallest address is one byte. you can''t right?
Advertisement
bools are usually represented as chars after compilation. 8 bits. bools. while it''s memory inefficient, it''s computationally much more efficient than storing bools as 1 bit.
well, if you want to build a 4 bit system on your own, you could store two nibbles (4 bits) in an unsigned char, and do stuff like

unsigned char nibble;
nibble = (#number#) & 0xf << 4;//put in upper 4 bits
nibble = (#number#) & 0xf;//put in lower 4 bits

int test = someInt + nibble & (0x0f); //add lower 4 bits
test = someInt + nibble & (0xf0); //add upper 4 bits


wrap that all into a class, and you could do it... though I don''t know why you would.
Here''s what I made since I was bored.

Use:
"const char PositionValue[] = { 128, 64, 32, 16, 8, 4, 2, 1};"

and "(somebyte & PositionValue) != 0)" can be used to get bit #i.

This topic is closed to new replies.

Advertisement