Leaders snarfblam Posted August 29, 2004 Leaders Posted August 29, 2004 Suppose I create a boolean array with 32 elements ("New Boolean(31) {}" in vb), will this take up four bytes for the array elements or are the boolean elements not guarenteed to take up only a single bit? Quote [sIGPIC]e[/sIGPIC]
Joe Mamma Posted August 29, 2004 Posted August 29, 2004 Suppose I create a boolean array with 32 elements ("New Boolean(31) {}" in vb)' date=' will this take up four bytes for the array elements or are the boolean elements not guarenteed to take up only a single bit?[/quote'] I believe the will be bytes. . . 8 bits. BTW have you considered an Flag attributed Enumeration???? [Flags] public enum AppFlag { AppFlagEmpty, AppFlag1, AppFlag2, AppFlag3, AppFlag4, AppFlag5 } used as such: private void button1_Click(object sender, System.EventArgs e) { AppFlag aFlag = AppFlag.AppFlagEmpty; aFlag |= AppFlag.AppFlag1; aFlag |= AppFlag.AppFlag3; if ( (aFlag & AppFlag.AppFlag3) != 0 ) MessageBox.Show("Flags contain: " + AppFlags.AppFlag.AppFlag3); else MessageBox.Show("Flags does not contain: " + AppFlag.AppFlag3); aFlag |= AppFlag.AppFlag2; aFlag |= AppFlag.AppFlag4; if ( ((aFlag & AppFlag.AppFlag3) & (aFlag & AppFlag.AppFlag4)) != 0) MessageBox.Show(string.Format("Flags contain {0} and {1}", AppFlag.AppFlag3, AppFlag.AppFlag4)); else MessageBox.Show(string.Format("Flags do not contain {0} and {1}", AppFlag.AppFlag3, AppFlag.AppFlag4)); } Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Wile Posted August 29, 2004 Posted August 29, 2004 If you are sure you want the array to have the exact lengt of 32, you might want to check out the BitVector32 (located in System.Collections.Specialized namespace). It is specialized for storing exactly 32 separate bits in as little memory space as possible (4 bytes). It got some powerfull methods to access the bits that might be usefull. Else a BitArray uses a bit more overhead (like all collection classes) but it does have some features that may save you some work. My guess on the memory usage of a regular array of booleans, is that it isnt optimized. I wouldn't be surprised if it used 32 bytes, or even 128 bytes (if all memory is allocated on 4 byte borders). But I'm not that deep into the .NET framework so don't take my word for it ;). Quote Nothing is as illusive as 'the last bug'.
Leaders snarfblam Posted August 30, 2004 Author Leaders Posted August 30, 2004 First of all, no, the number of booleans wont actually be 32. It will be 1024 booleans (most likely). And in comparison to the 128 bytes of booleans that would be the 8 (or 12, im not sure) bytes of overhead data isn't incredibly signifigant, so im not worried about that. These are not flags and even if using the flagged attribute is more memory efficient I doubt it would be worth the trouble; I am using the array of booleans as just that and picking the right flag from the enumeration would introduce unneccesary switches or ifs. It would be easier (at least in my opinion) just to use int32s and to check a bit just use the bit shift operator (i.e. 1 << bit_number) and AND that value with the integer. Anyways, thanks for the suggestions. Quote [sIGPIC]e[/sIGPIC]
Leaders snarfblam Posted September 16, 2004 Author Leaders Posted September 16, 2004 This is information pertaining to my origional question... According to the Visual Basic Language Reference... Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False. Also... In the full .NET Framework, the default marshaling for a System.Boolean corresponds to the MarshalAs(UnmanagedType.Bool) attribute, which uses a 4-byte integer value. If you are looking for memory efficiency, based on what I found I would not recommend the Boolean data type, but rather use the individual bits of an integer. Quote [sIGPIC]e[/sIGPIC]
Administrators PlausiblyDamp Posted September 16, 2004 Administrators Posted September 16, 2004 You may find the System.Collections.BitArray class an easy way to work with booleans - the constructor just accepts the number of bits you need and it provides array like access to each bit. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.