We can use integers to represent arrays of boolean flags. With bitwise operators we set certain bits to zero or 1. This saves memory.
With the extension methods here, we can set or get bits by a position. This can improve algorithms or even make new data structures possible.
Consider a 32-bit int
that has the decimal value 0—it is all zeros. If we set the bit at index 0, it is then equal to 1 in decimal form.
Input: 00000000000000000000000000000000 SetBitTo1(0): 00000000000000000000000000000001 SetBitTo1(1): 00000000000000000000000000000011
This program uses GetIntBinaryString
, which helps us by displaying bits. We introduce the BitExtensions
class
, which has 4 extension methods. We can set bits to 0 or 1.
BitExtensions
class
to test them. We set bits to 0 and 1, and then test bits for 0 and 1.SetBitTo0
receives the integer that stores the bits and the position of the bit we want to set to zero.SetBitTo0
is at first hard to understand. It uses the constant one and shifts it by the value of the position.SetBitTo0
takes the complement, which creates a mask. With bitwise AND on the value and the mask, we erase a one in the position.using System; static class BitExtensions { public static int SetBitTo1(this int value, int position) { // Set a bit at position to 1. return value |= (1 << position); } public static int SetBitTo0(this int value, int position) { // Set a bit at position to 0. return value & ~(1 << position); } public static bool IsBitSetTo1(this int value, int position) { // Return whether bit at position is set to 1. return (value & (1 << position)) != 0; } public static bool IsBitSetTo0(this int value, int position) { // If not 1, bit is 0. return !IsBitSetTo1(value, position); } } class Program { static void Main() { int value = 0; Console.WriteLine("{0} = {1}", GetIntBinaryString(value), value); value = value.SetBitTo1(0); Console.WriteLine("{0} = {1}", GetIntBinaryString(value), value); value = value.SetBitTo1(1); Console.WriteLine("{0} = {1}", GetIntBinaryString(value), value); value = value.SetBitTo1(3); Console.WriteLine("{0} = {1}", GetIntBinaryString(value), value); // Write whether some bits are set to 1. Console.WriteLine(value.IsBitSetTo1(0)); Console.WriteLine(value.IsBitSetTo1(1)); Console.WriteLine(value.IsBitSetTo1(23)); // Set bit 0 to 0. value = value.SetBitTo0(0); Console.WriteLine("{0} = {1}", GetIntBinaryString(value), value); // Bit 0 is not 1 now. Console.WriteLine(value.IsBitSetTo1(0)); // Should be true. Console.WriteLine(value.IsBitSetTo0(0)); Console.WriteLine(value.IsBitSetTo0(20)); } static string GetIntBinaryString(int value) { return Convert.ToString(value, 2).PadLeft(32, '0'); } }00000000000000000000000000000000 = 0 00000000000000000000000000000001 = 1 00000000000000000000000000000011 = 3 00000000000000000000000000001011 = 11 True True False 00000000000000000000000000001010 = 10 False True True
These bit methods can help for optimization purposes. If you have a huge data structure, you can use integers to represent sets of 32 flags of 1 or zero.
It is possible to use bitwise operators to set any bit in an integer to 1 or 0. This enables complex data structures that require large amounts of memory to run efficiently.