What size is the System.Boolean data type? Is it 4 bytes?

by Dominic Zukiewicz 23. January 2008 14:25
A simple question.

The bool values are true or false. Easy yes?

How many bytes does it occupy? 4 bytes? 2 bytes? 1 byte?

The actual answer is 1 byte, which you may expect, but the MCTS Application Development Framework exam states that the width of this field is 4 bytes!

So how do we settle it? Even easier.

Console.WriteLine("bool is {0} byte(s) long.", sizeof(bool));
//bool is 1 byte(s) long.

Console.WriteLine("System.Boolean is {0} byte(s) long.", sizeof(System.Boolean));
//System.Boolean is 1 byte(s) long.

Tags:

Framework

Comments

12/26/2008 8:16:23 PM #

A bool is actually only 1 byte, but alignment may cause 4 bytes to be used on a 32-bit platform, or even 8 bytes on a 64-bit platform.

4-byte variables (in 32 bit systems) works faster than 2-byte ones. This is the reason why you should use int instead of byte or short for counters and the like.

Felipe V. Rigo |

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar