I learned that a boolean does not need all the memory space it is provided in order to display true or false (1 or 0), but since the lowest space computers can work with is byte it was expressed as that.

I also read that there are ways to store the boolean in a bit instead of 8 bits, I got this code:

bool bits[] = {1, 0, 0, 0, 0, 0, 1, 0};
    
char character = 0;
    
for(int i = 0; i < 8; i++)     
    character += bits[i] << i;

I have seen that character becomes 65 at the end, I understand that this is because for each array member there is a 2 to the i value, if the member's value is 1 then this 2 to the i is added to character, since the first and second to last are actives the result is 65, I understand this.

But what I want to know is what is << doing exactly, I read something about moving the binary number, but since 1 is 1 in binary wouldn't this make it 1? And then the other 1 would not be 1 000 000? Why is that that it becomes 64?

Recommended Answers

All 3 Replies

But what I want to know is what is << doing exactly, I read something about moving the binary number

<< is a left shift. From the linked article: "Shifting left by n bits on a signed or unsigned binary number has the effect of multiplying it by 2^n."

but since 1 is 1 in binary wouldn't this make it 1? And then the other 1 would not be 1 000 000? Why is that that it becomes 64?

In your example, the value of bits[i] is always 0 or 1, and i is the position of each bit, so shifting bits[i] left by i will get you the numeric value that bit represents in a byte.

Break it down:

The statement bits[] = {1, 0, 0, 0, 0, 0, 1, 0} is the same as

bits[0] = 1;
bits[1] = 0;
bits[2] = 0;
bits[3] = 0;
bits[4] = 0;
bits[5] = 0;
bits[6] = 1;
bits[7] = 0;

And the loop with character += bits[i] << i; is the same as

char character = 0;
character += bits[0] << 0;
character += bits[1] << 1;
character += bits[2] << 2
character += bits[3] << 3;
character += bits[4] << 4;
character += bits[5] << 5;
character += bits[6] << 6;
character += bits[7] << 7;

Substituting the values of the bits array:

char character = 0;
character += 1 << 0;
character += 0 << 1;
character += 0 << 2
character += 0 << 3;
character += 0 << 4;
character += 0 << 5;
character += 1 << 6;
character += 0 << 7;

Replacing the << operator with its mathematical equivalent (this is now pseudocode):

char character = 0;
character += 1 * 2^0;
character += 0 * 2^1;
character += 0 * 2^2;
character += 0 * 2^3;
character += 0 * 2^4;
character += 0 * 2^5;
character += 1 * 2^6;
character += 0 * 2^7;

Exponentiate:

char character = 0;
character += 1 * 1;
character += 0 * 2;
character += 0 * 4;
character += 0 * 8;
character += 0 * 16;
character += 0 * 32;
character += 1 * 64;
character += 0 * 128;

Multiply:

char character = 0;
character += 1;
character += 0;
character += 0;
character += 0;
character += 0;
character += 0;
character += 64;
character += 0;

Simplify:

char character = 0;
character += 1;
character += 64;

So character = 0 + 1 + 64 = 65.

Thank you very much! I finally understand why the result!

Regarding what I wrote at the beginning about a boolean being 1 bit, I saw that I can use this:

struct BooleanStruct
{
    bool b1: 1, b2: 1, b3: 1, b4: 1, b5: 1, b6: 1, b7: 1, b8: 1;   
};

int main(int argc, char *argv[])
{
    BooleanStruct booleans;

    booleans.b1 = true; // and so on
}

Would this effectively make 8 booleans using just 1 byte?

If this is correct it leads me to my next question:

Is there a way to use some looping in order to set each boolean?

>>Would this effectively make 8 booleans using just 1 byte?
In theory, yes. But the behavior of bit fields is implementation specific. The best way to tell for sure is to display the result of a call to sizeof(BooleanStruct).

>>Is there a way to use some looping in order to set each boolean?
As created, there is not a way that I can think of. If you created function-like macros that used an "index" to perform a bit shift, it would possible; but I'm not familiar enough with the technique to be able to provide a quality example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.