Note that there are some explanatory texts on larger screens.

plurals
  1. POneed some clarification Java's bit representation of integers
    primarykey
    data
    text
    <p>This could be a very basic question. but I want to understand basic concepts clearly first. The question is about the bit representation of integers in java.</p> <p>In Java integers are represented in <code>32-bits</code>. <code>int y = 3</code>; is represented as <code>00000000 00000000 00000000 00000011</code></p> <p>The <code>32nd bit</code> is the signed bit, which is used to indicate the sign (0 if + and 1 if -ve) what I don't understand is how and why the integers are wrapped when signed: example:</p> <pre><code>y —-&gt;00000000 00000000 00000000 00000011 //3 x — &gt; 11111111 11111111 11111111 11111101 // -3 </code></pre> <p>while it should be: <code>10000000 00000000 00000000 00000011 // -3</code></p> <p>when it is <code>3://00000000 00000000 00000000 00000011,</code></p> <p>I could get the decimal value by <code>1^0 + 2 ^1</code>, by looking at the turned on bits</p> <p>when it is <code>-3://11111111 11111111 11111111 11111101</code> I see it is not easy to calculate the the decimal value. if -3 is represented as <code>10000000 00000000 00000000 00000011:</code> using the first bit, I get -ve, and the usual decimal calculation can be done to get 3</p> <p>similarly for the integer max with -ve sign only the 1st and 32nd bit are turned on:</p> <pre><code>-2147483647 —&gt; 10000000 00000000 00000000 00000001 -2 —&gt; 11111111 11111111 11111111 11111110 -1 —&gt; 11111111 11111111 11111111 11111111 </code></pre> <p>I dont understand how to read these bits to get the decimal value or how exactly is -1 represented by turning on all the bits.</p> <p>Thanks for lending help.</p>
    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.
 

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