Boolean type is 2 bytes????

  • Thread starter Thread starter Stephen
  • Start date Start date
S

Stephen

Why is the Boolean type 2 bytes when all it takes is 1
bit? I understand the Framework not having the ability
to reserve 1 bit (even though I think it would be nice to
have 8 bools in one byte), but 2 bytes? Why?? Why on
earth would anyone use that instead of just declaring
everything as a byte instead and using 1 and 0??

Stephen
 
Stephen,
Why is the Boolean type 2 bytes when all it takes is 1
bit?

A Boolean is actually just one byte. There are some parts of the
VB.NET documentation that says two, but that's incorrect.



Mattias
 
Then why is this:

Dim a as Boolean
MsgBox(Len(a))

Result is 2
When a is Byte, result is 1
 
Hello Stephen,

Interop size of a boolean might be 4 bytes (it's marshalled to Win32 BOOL,
I believe). But the CLR stores booleans as one byte each within GC'd
objects. This is spec'd in ECMA-335, probably Partition II

From a programmer's perspective, it really does not matter how many bytes
it occupies. But I think there may be several reason for adopting 2 bytes
in .NET. First, it makes easier translation to and from other technologies,
for example COM, InterOp, VC. There may be other considerations as well
such as faster execution at the machine code level, in a comparison
operation. Storing it in just one bit may save some space, but it will cost
more time in calculation.

Hope that helps.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!Content-Class: urn:content-classes:message
!From: "Stephen" <[email protected]>
!Sender: "Stephen" <[email protected]>
!References: <[email protected]>
<#[email protected]>
!Subject: Re: Boolean type is 2 bytes????
!Date: Sun, 31 Aug 2003 22:59:06 -0700
!Lines: 29
!Message-ID: <[email protected]>
!MIME-Version: 1.0
!Content-Type: text/plain;
! charset="iso-8859-1"
!Content-Transfer-Encoding: quoted-printable
!X-Newsreader: Microsoft CDO for Windows 2000
!Thread-Index: AcNwTieNZ4VVotJ7SVK1lNkGoH1gdQ==
!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!Newsgroups: microsoft.public.dotnet.framework
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:52683
!NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
!X-Tomcat-NG: microsoft.public.dotnet.framework
!
!Then why is this:
!Dim a as Boolean
!MsgBox(Len(a))
!Result is 2
!When a is Byte, result is 1
!>-----Original Message-----
!>Stephen,
!>
!>>Why is the Boolean type 2 bytes when all it takes is 1
!>>bit?
!>
!>A Boolean is actually just one byte. There are some
!parts of the
!>VB.NET documentation that says two, but that's incorrect.
!>
!>
!>
!>Mattias
!>
!>--
!>Mattias Sjögren [MVP] mattias @ mvps.org
!>http://www.msjogren.net/dotnet/
!>Please reply only to the newsgroup.
!>.
!>
!
 
Back
Top