• Advertisement

Archived

This topic is now archived and is closed to further replies.

bool bit dumb

This topic is 5155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

  • Advertisement