Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging integer bits
    primarykey
    data
    text
    <p>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 <br/> I'm trying with <br/> N = 5 (101)<br/> P = 2 (takes the 1)<br/> V = 0 (has to make it a zero)<br/> 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)<br/> the program is in the making so I'm currently working on <strong>V = 0</strong> so don't try with 1 <br/>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"<br/> - No, I don't want you to solve me the problem I want to know <strong>why after int result = mask &amp; integerBinary; I get 97 and not 1</strong></p> <pre><code>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) &amp;&amp; int.TryParse(positionLine, out position) &amp;&amp; int.TryParse(valueLine, out value)) // Try to parse the strings as integers { int integerBinary = int.Parse(Convert.ToString(integer, 2)); int bitValue = ((1 &lt;&lt; position) &amp; integerBinary) &gt;&gt; position; int mask = ~(1 &lt;&lt; position); if (value==0) { int result = mask &amp; integerBinary; Console.WriteLine("(n) After bit conversion = {0}", result); } else Console.WriteLine("(n) After bit conversion = {0}", integer); } else { Console.WriteLine("Invalid input."); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload