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.");
       }
    }
}
Konrad Kokosa

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Changing integer bits

From Dev

Shifting bits in an integer without changing the penultimate bit in C++?

From Dev

Changing type without changing bits

From Dev

the bits in R to express integer

From Dev

Reverse bits of a unsigned integer

From Dev

display the 16 bits for the integer

From Dev

display the 16 bits for the integer

From Dev

Left shift an integer by 32 bits

From Dev

How to put integer bits in a float?

From Dev

packing bits into an integer in C++

From Dev

Storing bits from an array in an integer

From Dev

Forming a pattern of bits from a integer

From Dev

packing bits into an integer in C++

From Dev

Binary representation of a 32 bits integer

From Dev

C# reversing the bits of an integer

From Dev

Group by on bits of an integer column in mysql

From Dev

Changing the integer in the String in Java

From Dev

array changing to integer

From Dev

Changing integer value of Pair(String, Integer) in dictionary

From Dev

Fastest function to set bits to one between two bits in an unsigned integer

From Dev

Which way is better to get lower 32 bits of a 64 bits integer

From Dev

Changing endianness on 3 byte integer

From Dev

Rails Migration for Changing String to Integer

From Dev

Java integer ++i not changing the value

From Dev

Getting the value of a constantly changing integer

From Dev

Rails Migration for Changing String to Integer

From Dev

Getting the value of a constantly changing integer

From Dev

sapply and lapply for changing integer to factor

From Dev

Number of value representation bits in a integer, according to the standard?