I would like to know why do most programming languages (including VB.NET) reserve 2 bytes for the boolean data type while only 1byte is sufficient.
I was told that this is due to the nature of most existent processors which are 32 bit processors. I was also told that such processors would have to convert a 1-byte data value to a 2-bytes data value to be able to process them. I would like to get more details on why is that the case. Is it cz the upper halves of the registers are 2 bytes wide, & it would be a waste if we didnt make good use of them? Or is this related to bus width? could it also be related to endianess, or a combination of the previous? if so how?
I know this is not a strictly programming answer, but i was hoping that a senior programmer would be able to answer it.
Any information is greatly appreciated (even if the issue is purely related to hardware)
Thank you very much in advance.
hrm, i think ie. MS VC6 uses one byte.
current x86 CPUs do have usable 8 bit registers (lowest byte of 32bit register).
IIRC, it would only make sense to use a 32bit variable (4 bytes) instead of anything smaller. it had to do either with the CPU always reading 32bits anyways, access being faster when aligning your data to have adresses divisible by 4, with cache issues (cycle wasting), or all this together. can't remember that precisely, has been a while . . . sorry :)
I have never heard of a compiler using 2 bytes for boolean values, but this might be due to the fact that I'm being pretty ignorant about performance issues concerning boolean variables ;-)
C# uses 1 byte for a boolean, so VB.net must use 1 byte as well? In fact the more you can compact a boolean, the faster things are generally speaking, because accessing additional non-cached memory is one of the slowest things you can do in modern computers. They did not make it a bit, I suppose, because there were not enough people using more than mulitple booleans in 1 data type. The alignment cost is far outweighed by cache miss/paging issues when you use too much memory.
It is not so much that a 1 byte value must be converted to a two byte value, it is that it may not be aligned on a byte multiple considered fast or acceptable by the CPU (typically 4 byte alignment on 32-bit CPU), so it may have to be shifted down in the register before being processed.