Boolean values

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
Zedtho
Posts: 189
Joined: February 14th, 2017, 7:32 pm

Boolean values

Post by Zedtho » June 25th, 2017, 2:02 pm

I've got a question about boolean values. Apparently, they take up one byte of space. Shouldnt they be able to be just one bit of space though? 0 for false and 1 for true or something? If these values are stored as strings as it looks like at the moment, that'd be kind of stupid in my opinion.
Hope this question isn't stupid!

xu2201g
Posts: 33
Joined: April 19th, 2017, 12:49 pm
Location: Germany

Re: Boolean values

Post by xu2201g » June 25th, 2017, 2:53 pm

Thats not a stupid question. It is right that bool values usually should take up only 1 bit of space, but the smallest unit a cpu can load from RAM is a Byte (8bit). So you have to send at least 1 Byte to the cpu even if it holds less information.

If you dont want to waste the other 7 bits you can use bitwise operations to handle up to 8 boolean values within 1 Byte:

For example you want to describe what components a entity (like a player entity or enemy entity etc.) have with booleans. You can use a bitmask for that.

index:
0 - represents position
1 - sprite
2 - state
3 - movable
4-7 - unused

7 6 5 4 3 2 1 0 - index of the Byte
0 0 0 0 1 0 1 1 - actual bitmask of an entity

So as u can see this entity has position, sprite and is movable. But how to access this information?
There come bitwise operatins in handy.

If u want to know if this entity has a position for example, you need another byte with only that flag u want to investigate as true.
That would be 0 0 0 0 0 0 0 1 and you can put them together with the bitwise operator & unlike &&.
Like you may assume bitwise means those 'And' checks ll be done for every bit and puts the result in the related index.

So for our example it would be:
0 0 0 0 1 0 1 1 - the entity
& & & & & & & &
0 0 0 0 0 0 0 1 - the position mask
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
0 0 0 0 0 0 0 1 - the result, which is true

So the result is true and we can say our entity has a position component and we can handle it likewise.
So how to create those bitmasks?
Every digit of the byte represents a power of 2 (2 ^ index). So u can use for example a char and set it to 1 (== 2^0) to turn on the most right digit for example. If u want to turn on the most left you need to set it to 128).

here is a short example:

Code: Select all

	unsigned char byteA = 1; //0000 0001
	unsigned char byteB = 128; // 1000 0000

	unsigned char bitmask = 1 + 8 + 64; // 0100 1001

	std::cout << (bool)(byteA & byteB) << std::endl; //returns false
	std::cout << (bool)(byteA & bitmask) << std::endl; //returns true
	std::cout << (bool)(byteB & bitmask) << std::endl; //returns false
Hope this is understandable, iam not a teacher or something like that.
Here is an overview about the stuff you can do with that:
https://en.wikipedia.org/wiki/Bitwise_operations_in_C

User avatar
Zedtho
Posts: 189
Joined: February 14th, 2017, 7:32 pm

Re: Boolean values

Post by Zedtho » June 25th, 2017, 7:05 pm

Thanks a lot for the detailed explanation! That makes a lot of sense.
Just one question,
would those 7 bits ever be useful to be used in a real world scenario?

xu2201g
Posts: 33
Joined: April 19th, 2017, 12:49 pm
Location: Germany

Re: Boolean values

Post by xu2201g » June 25th, 2017, 9:59 pm

It is useful to avoid tons of boolean variables to describe something and makes it easier to handle functions using those.

instead of

Code: Select all


bool a;
bool b;
bool c;
bool d;
bool e;
bool f;
bool g;
bool h;

foo(a,b,c,d,e,f,g,h);
where you would waste 8 * 7 bits and its getting even worse if u want to use like 32 bools (unsigned int).
you can use a bitfield:

Code: Select all

unsigned char field;

foo(field);
What exactly those bools represent depends on the problem you re trying to solve like the
userinput with a controller and every bool stands for a key being pressed for example or like the style of a window you re creating with flags/booleans for borderless, minimized, fullscreen etc. and those components an entity can have as i mentioned in my last post.

here some more infos about bitfields and their use;
general: https://en.wikipedia.org/wiki/Bit_field
more detailed: http://www.learncpp.com/cpp-tutorial/3- ... bit-masks/

I use bitfields not often tbh, but its good to know about them and the chapter iam reading in my sfml book was using those so i thought i can share it with u guys.

If u have only 2 bool variables or so then its alright to stick to them instead of using a bitfield.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Boolean values

Post by albinopapa » June 25th, 2017, 11:46 pm

Bit manipulation is also useful for other stuff, but as the context of this discussion, say you wanted to write for a mobile device or something that is memory limited, then it would be very useful in the real world. On PC's though, it's probably a waste of time to reduce bools/flags down to bits.

Chili, in his old tuts, covered alpha blending of pixels wherein he used bit masking and shifting to get the individual 8 bit unsigned chars from the 32 bit unsigned ints. Check out his Color class in the current framework. The get and set functions deal with bit manipulations. I know it's not bools/flags, but it's the same thing really, 4 chars packed into an int.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
Zedtho
Posts: 189
Joined: February 14th, 2017, 7:32 pm

Re: Boolean values

Post by Zedtho » June 26th, 2017, 10:03 am

Yeah, that makes a lot more sense. Thanks a lot for answering my questions and for the detailed answers!

Post Reply