Size? Boolean vs Byte
-
Just curious. From my recent reading ("Coding Techniques for Microsoft Visual Basic.Net" - Table 4-1), I noticed that a Boolean variable took 4 Bytes of storage and a Byte variable only took 1 Byte. It's clear why a Byte equals a Byte, but why on earth does a Boolean (who's only values can be either a 1 or 0) require 4 Bytes? Was this a typo? Thanks, Karen Nooobie to OOP and VB.Net 2005
-
Just curious. From my recent reading ("Coding Techniques for Microsoft Visual Basic.Net" - Table 4-1), I noticed that a Boolean variable took 4 Bytes of storage and a Byte variable only took 1 Byte. It's clear why a Byte equals a Byte, but why on earth does a Boolean (who's only values can be either a 1 or 0) require 4 Bytes? Was this a typo? Thanks, Karen Nooobie to OOP and VB.Net 2005
A boolean in the .NET Framework is not just a single bit, but is a structure with a couple fields and a bunch of methods. Internally, the boolean value is stored at an Int32, or 32-bit value, I think(!). RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Just curious. From my recent reading ("Coding Techniques for Microsoft Visual Basic.Net" - Table 4-1), I noticed that a Boolean variable took 4 Bytes of storage and a Byte variable only took 1 Byte. It's clear why a Byte equals a Byte, but why on earth does a Boolean (who's only values can be either a 1 or 0) require 4 Bytes? Was this a typo? Thanks, Karen Nooobie to OOP and VB.Net 2005
The system memory bus is 4 bytes (32 bits). The minimum packet of information along this bus then is 4 bytes. The common language runtime decides the actual size of the boolean at runtime. Typically it will use 4 bytes as this is the most efficient. If memory is at a premium, then it can reduce the size on the fly. On 64 bit architecture it will use 8 bytes.
-
The system memory bus is 4 bytes (32 bits). The minimum packet of information along this bus then is 4 bytes. The common language runtime decides the actual size of the boolean at runtime. Typically it will use 4 bytes as this is the most efficient. If memory is at a premium, then it can reduce the size on the fly. On 64 bit architecture it will use 8 bytes.
Thanks to all. My goal is to learn the right way to design tight classes. Soooo on today's 32 bit system, using less than 4 Bytes is inefficient. This is beginning to make sense -- I also read using a 'int16' or 'short integer' (2 Bytes) is less efficient than using an 'int32' or 'integer' (4 Bytes). I was using the 'Byte' declaration for integer variables that would never see a value higher than 255. I understand this to be slower than just using the 'integer' declaration. Right? Thanks, Karen Nooobie to OOP and VB.Net 2005
-
Thanks to all. My goal is to learn the right way to design tight classes. Soooo on today's 32 bit system, using less than 4 Bytes is inefficient. This is beginning to make sense -- I also read using a 'int16' or 'short integer' (2 Bytes) is less efficient than using an 'int32' or 'integer' (4 Bytes). I was using the 'Byte' declaration for integer variables that would never see a value higher than 255. I understand this to be slower than just using the 'integer' declaration. Right? Thanks, Karen Nooobie to OOP and VB.Net 2005
watagal wrote: I was using the 'Byte' declaration for integer variables that would never see a value higher than 255. I understand this to be slower than just using the 'integer' declaration. Right? Not necessarily. Depends on what math your doing and what the result is going to end up in. But, you also don't ahve to be THAT tight with it. The size of the variable isn't really a concern because of the VAST memories of today's systems. What makes a "tight" class is the use of proper OOP techniques, data types, logic, class organization and relationships to other classes. At least IMHO. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome