Bitwise Set Bit ExampleUse extension methods to set a bit in an integer to zero or one, and then test the bits in the integer.
C#
Set bit. We can use integers to represent arrays of boolean flags. With bitwise operators we set certain bits to zero or 1. This saves memory.
Method info. 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.
Extension
Input and output. 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.
Start Here we call the methods in the BitExtensions class to test them. We set bits to 0 and 1, and then test bits for 0 and 1.
Note SetBitTo0 receives the integer that stores the bits and the position of the bit we want to set to zero.
Bitwise Complement
Binary Representation
Note 2 The code for SetBitTo0 is at first hard to understand. It uses the constant one and shifts it by the value of the position.
Then SetBitTo0 takes the complement, which creates a mask. With bitwise AND on the value and the mask, we erase a one in the position.
Bitwise
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 n) { char[] b = new char[32]; int pos = 31; int i = 0; while (i < 32) { if ((n & (1 << i)) != 0) b[pos] = '1'; else b[pos] = '0'; pos--; i++; } return new string(b); } }
00000000000000000000000000000000 = 0 00000000000000000000000000000001 = 1 00000000000000000000000000000011 = 3 00000000000000000000000000001011 = 11 True True False 00000000000000000000000000001010 = 10 False True True
A discussion. 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.
Then As you act upon this data structure, you can set certain flags to keep track of the meaning of a certain node.
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.