Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Boolean 4 bytes...?

Boolean 4 bytes...?

Scheduled Pinned Locked Moved The Lounge
question
25 Posts 11 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A alex barylski

    Why is it boolean is 4 bytes in most languages...? This makes no sense, theres 31 bits going to waste...??? Is this so you can perform some fancy bit shifting, ANDing, ORing and store 32 on/off's in one bool...? Just curious! "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

    T Offline
    T Offline
    Todd C Wilson
    wrote on last edited by
    #4

    Eh, this depends on the compiler vendor and the platform. With Microsoft, you can twiddle the #pragma pack statement to change this. Or use the unsigned int xyz:1 statement. Or better yet, post in the right forum. In any case, the alignment of 32 bits (64 on some, 16 on others) is designed to keep the data cache aligned up nicely.


    Visual Studio Favorites - www.nopcode.com/visualfav

    A 1 Reply Last reply
    0
    • S SimonS

      Tim Smith wrote: 4 bytes are the fastest value to fetch from memory I know about 32bit/64bit systems, but I don't really understand why it would be faster. Please explain (I haven't quite finished my 8th Phd ). Cheers, Simon X-5 452 rules.

      T Offline
      T Offline
      Todd C Wilson
      wrote on last edited by
      #5

      You can get all the dirty details at intel.com in their processor whitesheets, but basically, it has to do with the alignement of the data for the cache. The cache is optimized for 32-bit words for x86 processors.


      Visual Studio Favorites - www.nopcode.com/visualfav

      1 Reply Last reply
      0
      • A alex barylski

        Why is it boolean is 4 bytes in most languages...? This makes no sense, theres 31 bits going to waste...??? Is this so you can perform some fancy bit shifting, ANDing, ORing and store 32 on/off's in one bool...? Just curious! "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

        H Offline
        H Offline
        Henry Jacobs
        wrote on last edited by
        #6

        DWORD alignment

        N 1 Reply Last reply
        0
        • H Henry Jacobs

          DWORD alignment

          N Offline
          N Offline
          NormDroid
          wrote on last edited by
          #7

          32bits! Normski. - Professional Windows Programmer

          M 1 Reply Last reply
          0
          • T Tim Smith

            For most systems these days, 4 bytes are the fastest value to fetch from memory. Tim Smith Descartes Systems Sciences, Inc.

            A Offline
            A Offline
            alex barylski
            wrote on last edited by
            #8

            True, but the compiler could pack 4 - 1 byte booleans into 4 byte variable. I've thought of the DWORD alignment for avoiding...whats the word i'm looking for..??? memory stalls...wasted clocks, but still seems like an aweful waste of space.. Well actually....@^$&^$%$%&*@$%$%@^*)^@$&*@$%%$(* fast forward duh...I guess 31 bits here and there isn't huge, but it does kinda not make sense. My best impression of funny noises a CD player makes when forwarding/rewinding kinda loks like a regex huh...? ;P "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

            K 1 Reply Last reply
            0
            • S SimonS

              Tim Smith wrote: 4 bytes are the fastest value to fetch from memory I know about 32bit/64bit systems, but I don't really understand why it would be faster. Please explain (I haven't quite finished my 8th Phd ). Cheers, Simon X-5 452 rules.

              A Offline
              A Offline
              alex barylski
              wrote on last edited by
              #9

              simons wrote: I haven't quite finished my 8th Phd ). Are you serious...? I haven't quite finished highschool... ;P So whats a guy with 8 phd's called...Doctor's doctor...? "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

              S R 2 Replies Last reply
              0
              • T Todd C Wilson

                Eh, this depends on the compiler vendor and the platform. With Microsoft, you can twiddle the #pragma pack statement to change this. Or use the unsigned int xyz:1 statement. Or better yet, post in the right forum. In any case, the alignment of 32 bits (64 on some, 16 on others) is designed to keep the data cache aligned up nicely.


                Visual Studio Favorites - www.nopcode.com/visualfav

                A Offline
                A Offline
                alex barylski
                wrote on last edited by
                #10

                Right forum...? This is a conceptual kinda sorta question...I wasn't asking WHY in Visual C++ it's like this...I know why...32 bit processor is why...DWORD alignment is why...i wanted to why...why...get others opinions why... I'm NOT baffled just curious why the compiler doesn't keep a count of bools and perfom the funny bit shifting ANDing ORing on the 32 bits instead of using mov to push and pull DWORDS around with only one active bit. It's not a waste of time, but rather of space. "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

                T 1 Reply Last reply
                0
                • A alex barylski

                  Right forum...? This is a conceptual kinda sorta question...I wasn't asking WHY in Visual C++ it's like this...I know why...32 bit processor is why...DWORD alignment is why...i wanted to why...why...get others opinions why... I'm NOT baffled just curious why the compiler doesn't keep a count of bools and perfom the funny bit shifting ANDing ORing on the 32 bits instead of using mov to push and pull DWORDS around with only one active bit. It's not a waste of time, but rather of space. "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

                  T Offline
                  T Offline
                  Todd C Wilson
                  wrote on last edited by
                  #11

                  No, it would be a waste of both time and space, because the processor will not be able to do something like (pseudo-code): loadregister address but will need to do: loadregister #0x01 barrelshift left bitposition andmask address barrelshift right bitposition You're trading off 3 extra bytes of data memory (which is 1 time in memory) for a minimum 4x increase of code for each access of that bit. Now, if your overriding concern is memory storage (such as a huge table of bit data), then it is your job, as the developer, to inform the compiler to pack them tigher, probably by using the afor-mentioned unsigned int xyz:1 statement. But you're trading off speed & code size to reduce data memory consumption.


                  Visual Studio Favorites - www.nopcode.com/visualfav

                  A 2 Replies Last reply
                  0
                  • N NormDroid

                    32bits! Normski. - Professional Windows Programmer

                    M Offline
                    M Offline
                    MarkyMark
                    wrote on last edited by
                    #12

                    This is a C++ BOOL you're talking about is it? a BOOL (int) is 4 bytes but a bool is only one.

                    A 1 Reply Last reply
                    0
                    • A alex barylski

                      simons wrote: I haven't quite finished my 8th Phd ). Are you serious...? I haven't quite finished highschool... ;P So whats a guy with 8 phd's called...Doctor's doctor...? "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

                      S Offline
                      S Offline
                      SimonS
                      wrote on last edited by
                      #13

                      HockeyDude wrote: Are you serious...? er... no. :-O :-O Cheers, Simon X-5 452 rules.

                      D A 2 Replies Last reply
                      0
                      • A alex barylski

                        Why is it boolean is 4 bytes in most languages...? This makes no sense, theres 31 bits going to waste...??? Is this so you can perform some fancy bit shifting, ANDing, ORing and store 32 on/off's in one bool...? Just curious! "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

                        B Offline
                        B Offline
                        Brigg Thorp
                        wrote on last edited by
                        #14

                        This is the kind of crap that make applications enormous. How many values could have been fit into a single boolean bit value, only to take up 32 bits instead. Imagine if I had a bit array, with 100 booleans. That is 3100 bytes (3.1Kb) of wasted space. This also happens with hard drive cluster size as well. Whose stupid idea was this anyway? No wonder all new computers are coming with 100+ Gb drives. Jeesh End of rant... Brigg Thorp Software Engineer Timex Corporation

                        D 1 Reply Last reply
                        0
                        • A alex barylski

                          simons wrote: I haven't quite finished my 8th Phd ). Are you serious...? I haven't quite finished highschool... ;P So whats a guy with 8 phd's called...Doctor's doctor...? "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

                          R Offline
                          R Offline
                          Ray Hayes
                          wrote on last edited by
                          #15

                          HockeyDude wrote: So whats a guy with 8 phd's called...Doctor's doctor...? Maybe! When I lived in Germany, I rented a flat from a local Uni Professor, his contact name/address started Herrn Professor Doktor Doktor Stroubler (maybe a couple of spelling mistakes there though, it is FRIDAY afternoon and I went to the pub at lunch :) !) Regards, Ray

                          A 1 Reply Last reply
                          0
                          • S SimonS

                            HockeyDude wrote: Are you serious...? er... no. :-O :-O Cheers, Simon X-5 452 rules.

                            D Offline
                            D Offline
                            dandy72
                            wrote on last edited by
                            #16

                            A distant relative of mine had at least 6 a few years ago. He had a few more going simultaneously, so he's probably over 8 by now. Mind you, he was the prime example of "a genius is someone who can do anything except earn a living".

                            1 Reply Last reply
                            0
                            • B Brigg Thorp

                              This is the kind of crap that make applications enormous. How many values could have been fit into a single boolean bit value, only to take up 32 bits instead. Imagine if I had a bit array, with 100 booleans. That is 3100 bytes (3.1Kb) of wasted space. This also happens with hard drive cluster size as well. Whose stupid idea was this anyway? No wonder all new computers are coming with 100+ Gb drives. Jeesh End of rant... Brigg Thorp Software Engineer Timex Corporation

                              D Offline
                              D Offline
                              dandy72
                              wrote on last edited by
                              #17

                              Imagine how quickly your hard drive would fragment if each file was allocated space on a per-byte basis...

                              1 Reply Last reply
                              0
                              • S SimonS

                                HockeyDude wrote: Are you serious...? er... no. :-O :-O Cheers, Simon X-5 452 rules.

                                A Offline
                                A Offline
                                alex barylski
                                wrote on last edited by
                                #18

                                :-D "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

                                1 Reply Last reply
                                0
                                • R Ray Hayes

                                  HockeyDude wrote: So whats a guy with 8 phd's called...Doctor's doctor...? Maybe! When I lived in Germany, I rented a flat from a local Uni Professor, his contact name/address started Herrn Professor Doktor Doktor Stroubler (maybe a couple of spelling mistakes there though, it is FRIDAY afternoon and I went to the pub at lunch :) !) Regards, Ray

                                  A Offline
                                  A Offline
                                  alex barylski
                                  wrote on last edited by
                                  #19

                                  Whatever the case...someone with 8 phd's would be something other than human. Cheers "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

                                  1 Reply Last reply
                                  0
                                  • A alex barylski

                                    True, but the compiler could pack 4 - 1 byte booleans into 4 byte variable. I've thought of the DWORD alignment for avoiding...whats the word i'm looking for..??? memory stalls...wasted clocks, but still seems like an aweful waste of space.. Well actually....@^$&^$%$%&*@$%$%@^*)^@$&*@$%%$(* fast forward duh...I guess 31 bits here and there isn't huge, but it does kinda not make sense. My best impression of funny noises a CD player makes when forwarding/rewinding kinda loks like a regex huh...? ;P "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

                                    K Offline
                                    K Offline
                                    Kastellanos Nikos
                                    wrote on last edited by
                                    #20

                                    I can send you an old 64MB SIMM for free. That's 16.777.216 booleans. ;P - - - - - - - - - - - - - - - - - - Memory leaks is the price we pay \0 01234567890123456789012345678901234

                                    1 Reply Last reply
                                    0
                                    • T Todd C Wilson

                                      No, it would be a waste of both time and space, because the processor will not be able to do something like (pseudo-code): loadregister address but will need to do: loadregister #0x01 barrelshift left bitposition andmask address barrelshift right bitposition You're trading off 3 extra bytes of data memory (which is 1 time in memory) for a minimum 4x increase of code for each access of that bit. Now, if your overriding concern is memory storage (such as a huge table of bit data), then it is your job, as the developer, to inform the compiler to pack them tigher, probably by using the afor-mentioned unsigned int xyz:1 statement. But you're trading off speed & code size to reduce data memory consumption.


                                      Visual Studio Favorites - www.nopcode.com/visualfav

                                      A Offline
                                      A Offline
                                      alex barylski
                                      wrote on last edited by
                                      #21

                                      Thats the response I was looking for. See initially i wasn't even thinking about the the clock cycles expended for shl, shr never mind the size of instruction(s). All I was thinking was that 4 bytes on a 32 bit processor fits like hand in glove, but those poor 31 remaining bits must get some lonely. Yes after double checking with intel docs it occured to me...IT's a waste of space yup, but it'd be more with the added instructions shl, shr. Hell I almost went to lengths of exemplifying my own mistake...I was gonna find the difference in clocks and and code size and show you how right you were, but decided showing someone how right they were cuz how wrong I was, isn't acceptable. Congrats...I'm never wrong...not that i'm a genius, I just don't speak unless I know what i'm talking about or perhaps with as much authority. Not that i was trying to be authoritive, just didn't quite find yoer comment about asking in the right forum appropriate. I've asked numerous conceptual, theory questions there. Thats what i was looking for, not technical...i figure thats what books are for, but in this case it proved beneficial. Ah well tis what i get for speaking before thinking. Ummm...I'm super tired...damn computer keeps me awake for days...I slept yesterday though...poor excuse for bad judgement I agree, but it's an excuse. ;P

                                      shl eax, 2

                                      Is only 2 cycles and 3 bytes so i wasn't "that" wrong...but I was wrong... Cheers! "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

                                      1 Reply Last reply
                                      0
                                      • M MarkyMark

                                        This is a C++ BOOL you're talking about is it? a BOOL (int) is 4 bytes but a bool is only one.

                                        A Offline
                                        A Offline
                                        alex barylski
                                        wrote on last edited by
                                        #22

                                        In Visual C++ 5.0 and later yup. Actually the question stemed from my old TurboPascal program, which uses 4 bytes for boolean also. "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

                                        1 Reply Last reply
                                        0
                                        • T Todd C Wilson

                                          No, it would be a waste of both time and space, because the processor will not be able to do something like (pseudo-code): loadregister address but will need to do: loadregister #0x01 barrelshift left bitposition andmask address barrelshift right bitposition You're trading off 3 extra bytes of data memory (which is 1 time in memory) for a minimum 4x increase of code for each access of that bit. Now, if your overriding concern is memory storage (such as a huge table of bit data), then it is your job, as the developer, to inform the compiler to pack them tigher, probably by using the afor-mentioned unsigned int xyz:1 statement. But you're trading off speed & code size to reduce data memory consumption.


                                          Visual Studio Favorites - www.nopcode.com/visualfav

                                          A Offline
                                          A Offline
                                          alex barylski
                                          wrote on last edited by
                                          #23

                                          However...It would ge quicker to pull 32 bits into say register eax. Then perform AND/OR with your 32 bit mask wouldn't it...? Something like this: boolean = 123 //1111011 array of bool's mov eax, [boolean] and eax, 0x00000001 //Mask off unwanted bits, first bit the above is the same effect as mov eax, [boolean] //But this time it holds only one bit assuming the mov eax, [boolean] consumes 6 bytes and 8 cycles or so and the and eax, 0x00000001 uses 6 bytes and 6 cycles, theres no perfomance gain, but if theres more than one bool.

                                          //Compiler way
                                          mov eax, [boolean1] //6 bytes, 8 clocks
                                          mov ebx, [boolean2] //6 bytes, 8 clocks
                                          mov edx, [boolean3] //6 bytes, 8 clocks
                                          mov ecx, [boolean4] //6 bytes, 8 clocks
                                          //24 bytes, 32 clocks + 16 bytes for four booleans

                                          //Optimized way
                                          mov eax, [boolean] //6 bytes, 8 clocks + 4 bytes for one boolean
                                          and eax, 0x00000001 //6 bytes, 6 clocks
                                          and ebx, 0x00000002 //6 bytes, 6 clocks
                                          and ecx, 0x00000003 //6 bytes, 6 clocks
                                          and edx, 0x00000004 //6 bytes, 6 clocks
                                          //30 bytes, 32 clocks, 4 bytes for boolean

                                          What am I missing...? Assuming the above data is somewhat accurate: the bit masking method saves space and if and uses fewer clocks like I suspect it might (maybe) then it would execute in less time so there is a time and space savings. "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

                                          T 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups