Go Back   kirupaForum > Development > Other Programming (Java, C, C++, etc.)

Reply
 
Thread Tools Display Modes
Old 11-28-2009, 06:46 PM   #1
NeoDreamer
¸,ø¤º°`°º¤ø,¸
 
NeoDreamer's Avatar
Location Palace of Auborn Hills

Posts 1,977
How is boolean represented in memory?

How are booleans represented in memory? If you had 8 booleans declared one after another, will they be packed tightly in 1 byte?

How are booleans retrieved? No OS will actually fetch 1 bit, right? They must grab an entire word (16 bits in Unix, I believe). If this is so, then wouldn't it be faster to use ints instead of bools? Because the bool would have to be extracted out of the retrieved word (with an XOR???) whereas the int can be used directly.

__________________
NeoDreamer is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 11-28-2009, 07:27 PM   #2
jase21
 
jase21's Avatar
Location Kerala, India

Posts 268
Theoretically a boolean consumes only 1 bit. But its represented using a byte of memory. Consider if there is only one boolean to be represented then the value will be either 0 or 1.
=>
X X X X X X X b ; b-> 0 or 1.

but if there are two or more boolean variables coming up, then it gets stored in the next location in the same byte of the memory but points to the respective variable.
=>
X X X X X X b1 b0 ; b0, b1 either 0 or 1. until the whole chunk gets filled.
Eventually it initializes the next chunk and so on.
jase21 is offline   Reply With Quote
Old 11-28-2009, 07:36 PM   #3
jase21
 
jase21's Avatar
Location Kerala, India

Posts 268
And how its extracted depends on the assembly codes (after compiler or assembler optimization).
AFAIK xor could be used or a compare can be used as well as test instruction etc..
jase21 is offline   Reply With Quote
Old 11-28-2009, 11:25 PM   #4
ajcates
Professor Angrypants
 
ajcates's Avatar
Location Vista, California

Posts 1,606
umm numbers on a computer are really actually bools strung together… this is called binary.

__________________

http://ajcates.com
ajcates is offline   Reply With Quote
Old 11-29-2009, 12:38 AM   #5
MTsoul
Bucket Kicker
 
MTsoul's Avatar
Location Vancouver, Canada

Posts 2,471
cout << sizeof(bool) ?

__________________
If you notice this notice you will notice that this notice is not worth noticing.

"Are you doing anything tonight? If not, how about me?"

Opera Sucks! - FIX IT
Oliver Zheng
MTsoul is offline   Reply With Quote
Old 12-06-2009, 03:28 AM   #6
last_hopes
Registered User
 
last_hopes's Avatar
How is boolean represented in memory

Hey, How can I read out a Car ID from the Memory? I tried it in that way:
last_hopes is offline   Reply With Quote
Old 12-07-2009, 03:07 PM   #7
jwilliam
Call me Rooster Cogburn.
I can't imagine that it would be more efficient to store booleans in an int... When you fetch data to put in a register I think it has to be 32/64 bits at a time. In the end, the gates that actually perform the logic are working on single bits of data anyway.
jwilliam is offline   Reply With Quote
Old 12-10-2009, 03:33 AM   #8
ajcates
Professor Angrypants
 
ajcates's Avatar
Location Vista, California

Posts 1,606
@jwilliam
It can be… look at unix file permissions.

__________________

http://ajcates.com
ajcates is offline   Reply With Quote
Old 12-10-2009, 05:40 PM   #9
jwilliam
Call me Rooster Cogburn.
I see what you're getting at, but the question was, is it more efficient to store a single boolean in an integer (at least 4 bytes) or in a single bit. If you declare an int in C and then set it to either 0 or 1, it's still going to take up sizeof(int) amount of space in memory.
jwilliam is offline   Reply With Quote
Old 12-10-2009, 06:19 PM   #10
Krilnon
≈ ≠ =
 
Krilnon's Avatar
Location Rochester, NY

Posts 7,183
Almost no instructions in any given instruction set deal with memory at the bit level, and you need to be able to address different boolean variables separately (especially if you want to pass separate pointers around). Compilers can choose to pack booleans into individual bits of a word, but it won't always be done.

If you were to have multiple booleans stored in a single word, there would be an added expense to reading and writing them. Some instruction sets can fetch a single bit from a word in a single instruction, but I don't personally know how common that instruction is. The processor would still fetch a whole word from memory, but it would mask off the rest of the bits once it was retrieved.

At any rate, it's great to store as many booleans as you can in a word if you're worried about memory consumption or bandwidth, but it's not really worth the runtime expense to pack and unpack those bits if you're just dealing with the limited amount of declared boolean variables that a programmer will use within almost any program. That's probably why C doesn't even have a boolean type and why almost any file format that stores large files will have some packed form for booleans.
Krilnon is offline   Reply With Quote
Old 12-14-2009, 04:18 AM   #11
MTsoul
Bucket Kicker
 
MTsoul's Avatar
Location Vancouver, Canada

Posts 2,471
On my system, sizeof(bool) == 1. It's the same size as a char. If you want space savings (and not zero overhead, in which case use bool), look up vector<bool>. It implements what Krilnon says, kind of.

__________________
If you notice this notice you will notice that this notice is not worth noticing.

"Are you doing anything tonight? If not, how about me?"

Opera Sucks! - FIX IT
Oliver Zheng
MTsoul is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:38 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com