darighteous1
darighteous1

Reputation: 89

C# How to remove the n-th bit in integer?

I am trying to find a way to remove a bit from an integer. The solution must not use string operations. For example, I have the number 27, which is 11011 in binary. I want to remove the third bit so it leaves me with 1011. Or we have 182 (10110110), remove the 6th bit so the result is 1110110 (which is 118). I am trying to think of the algorithm how to do that, but so far no luck, and I can't find useful information on the internet.

I know how to use bitwise operators and how to extract or manipulate bits in integers (change values, exchange values etc), but I don't know how to 'remove' a certain bit.

I am not looking for code, just the logic of the operation. If anyone could help me, that would be awesome!

Regards, Toni

Upvotes: 2

Views: 6146

Answers (6)

Buğra ARSLAN
Buğra ARSLAN

Reputation: 1

        int a = 27;//int= 4byte equal to 32 bit
        string binary = "";
        for (int i = 0; i < 32; i++)
        {             
            if ((a&1)==0)//if a's least significant bit is 0 ,add 0 to str
            {
                binary = "0" + binary;
            }
            else//if a's least significant bit is 1 ,add 1 to str
            {
                binary = "1" + binary;
            }
            a = a >> 1;//shift the bits left to right and delete lsb
            //we are doing it for 32 times because integer have 32 bit.
        }
        Console.WriteLine("Integer to Binary= "+binary);

     //Now you can operate the string(binary) however you want.

      binary = binary.Remove(binary.Length-4,1);//remove 4st bit from str

Upvotes: 0

Falk H&#252;ffner
Falk H&#252;ffner

Reputation: 5040

Here is a version that needs slightly fewer operations than the solution by harold:

x ^ (((x >> 1) ^ x) & (0xffffffff << n));

The idea is that below n, bits are xored with zero, leaving them unchanged, while from n and above the two x xored cancel each other out, leaving x >> 1.

Upvotes: 0

JLee
JLee

Reputation: 174

  int Place = 7;
  int TheInt = 182;

  string binary = Convert.ToString(TheInt, 2);
  MessageBox.Show(binary.Remove(binary.Length - Place, 1));

Upvotes: 0

user555045
user555045

Reputation: 64903

No problem, just decompose the number into the "upper part" and the "lower part", and put them together without the middle bit that now disappeared.

Not tested:

uint upper = x & 0xFFFFFFF0;
uint lower = x & 7;
return (upper >> 1) | lower;

More generally: (also not tested)

uint upper = x & (0xFFFFFFFE << n);
uint lower = x & ((1u << n) - 1);
return (upper >> 1) | lower;

Upvotes: 9

Tachyon
Tachyon

Reputation: 452

You could use the following approach:

int value = 27;
string binary = Convert.ToString(value, 2);
binary = binary.Remove(binary.Length-3-1,1);  //Remove the exact bit, 3rd in this case
int newValue = Convert.ToInt32(binary, 2);
Console.WriteLine(newValue);

Hope it helps!

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726849

In order to do this you need two bit masks and a shift.

The first bit mask gives you the portion of the number above bit n, exclusive of the n-th bit. The mask is constructed as follows:

var top = ~((1U<<(n+1))-1); // 1111 1111 1000 000, 0xFF80

The second bit mask gives you the portion of the number below bit n, exclusive of the n-th bit:

var bottom = (1U<<n)-1;     // 0000 0000 0011 1111, 0x003F

Comments above show the values for your second example (i.e. n == 6)

With the two masks in hand, you can construct the result as follows:

var res = ((original & top)>>1) | (original & bottom);

Demo.

Upvotes: 1

Related Questions