Home
C#
Bitwise Set Bit Example
Updated Jun 20, 2024
Dot Net Perls
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
Bitwise 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.
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
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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jun 20, 2024 (simplify).
Home
Changes
© 2007-2025 Sam Allen