Groups | Blog | Home
all groups > dotnet framework > august 2003 >

dotnet framework : Boolean type is 2 bytes????


Stephen
8/31/2003 10:35:40 PM
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
8/31/2003 10:59:06 PM
Then why is this:

Dim a as Boolean
MsgBox(Len(a))

Result is 2
When a is Byte, result is 1


[quoted text, click to view]
Mattias Sjögren
9/1/2003 7:45:23 AM
Stephen,

[quoted text, click to view]

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/
yhhuang NO[at]SPAM online.microsoft.com
9/1/2003 8:14:41 AM
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" <graybeal@bellatlantic.net>
!Sender: "Stephen" <graybeal@bellatlantic.net>
!References: <03b601c3704a$e1068ea0$a601280a@phx.gbl>
<#wpqbwEcDHA.856@tk2msftngp13.phx.gbl>
!Subject: Re: Boolean type is 2 bytes????
!Date: Sun, 31 Aug 2003 22:59:06 -0700
!Lines: 29
!Message-ID: <03d401c3704e$278d7d90$a601280a@phx.gbl>
!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.
!>.
!>
!
AddThis Social Bookmark Button