Иван Божков
Иван Божков

Reputation: 264

Changing integer bits

Ok, I have N = integer, P = position,V = 0 or 1 I have to change the bit at position P of integer N with the value V
I'm trying with
N = 5 (101)
P = 2 (takes the 1)
V = 0 (has to make it a zero)
and the result is 97 (1100 0001) (should be 0000 0001) I think the problem is from the mask because when I write it in the console its -5 (as it should be) but if I parse it, to see its bits I get an error (overflow)
the program is in the making so I'm currently working on V = 0 so don't try with 1
Yesterday I posted a question and a lot of people posted sh*t like "this is not a question - you want us to solve u your problem"
- No, I don't want you to solve me the problem I want to know why after int result = mask & integerBinary; I get 97 and not 1

using System;
class ChangeBit
{
    static void Main()
    {
        Console.Write("(n) Type an integer: ");
        string integerLine = Console.ReadLine(); // Read string from console
        Console.Write("(p) Type position: ");
        string positionLine = Console.ReadLine(); // Read string from console
        Console.Write("(v) Type 0 or 1: ");
        string valueLine = Console.ReadLine(); // Read string from console
        int value;
        int integer;
        int position;
        if (int.TryParse(integerLine, out integer) && int.TryParse(positionLine, out position) && int.TryParse(valueLine, out value)) // Try to parse the strings as integers
        {
            int integerBinary = int.Parse(Convert.ToString(integer, 2));
            int bitValue = ((1 << position) & integerBinary) >> position;
            int mask = ~(1 << position);
            if (value==0)
            {
            int result = mask & integerBinary;
            Console.WriteLine("(n) After bit conversion = {0}", result);
            }
            else Console.WriteLine("(n) After bit conversion = {0}", integer);
        }
        else
        {
        Console.WriteLine("Invalid input.");
       }
    }
}

Upvotes: 2

Views: 1758

Answers (4)

user5707327
user5707327

Reputation:

Try:

integer ^ ((-value ^ integer) & (1 << position))

This will check if the bit is set and, if so, will change its value using the bitwise operator ^.

Upvotes: 1

Patato
Patato

Reputation: 1472

this code has two problems first

int integerBinary = int.Parse(Convert.ToString(integer, 2));

does not need, cause the input integer is can be directly used to do logic operation, and this line does not mean convert integer to its binary format, after this line integer has become a different number

second

else Console.WriteLine("(n) After bit conversion = {0}", integer);

if value is 1 you still need to do some thing( if the original position is 0)

so the right code maybe

 if (int.TryParse(integerLine, out integer) && int.TryParse(positionLine, out position) && int.TryParse(valueLine, out value)) // Try to parse the strings as integers
            {

                int mask= (1<< position);
                int temp = mask | integer;
                int mask2 = ~((1-value)<<position);
                int result = mask2 & temp;
                result  = mask & result;
                Console.WriteLine("(n) After bit conversion = {0}", result);

            }

Upvotes: 0

Arnaud F.
Arnaud F.

Reputation: 8452

Much easier:

        if (int.TryParse(integerLine, out integer) && int.TryParse(positionLine, out position) && int.TryParse(valueLine, out value)) // Try to parse the strings as integers
        {
            BitArray a = new BitArray(BitConverter.GetBytes(integer));
            a.Set(position, value == 1);

            Console.WriteLine("(n) After bit conversion = {0}", a.GetInt32());
        }

With GetInt32 declared :

internal static class BitArrayEx
{
    internal static int GetInt32(this BitArray bitArray)
    {
        int[] array = new int[1];
        bitArray.CopyTo(array, 0);
        return array[0];
    }
}

Upvotes: 1

Konrad Kokosa
Konrad Kokosa

Reputation: 16878

You are mixing binary string representation with binary integers:

int integerBinary = int.Parse(Convert.ToString(integer, 2));

after this line integerBinary is 101 because you have converted it from binary string representation "101" of 5. After that all integers operation are invalid as such 101 makes no sense.

Upvotes: 0

Related Questions