Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are assuming that 101 is three bits long. Java doesn't support variable length bit operations, it operates on a whole <code>int</code> of bits, so <code>~</code> will be the <code>not</code> of a 32 bit long "101".</p> <p>--- Edited after being asked "How can I fix this?" ---</p> <p>That's a really good question, but the answer is a mix of "you can't" and "you can achieve the same thing by different means".</p> <p>You can't fix the <code>~</code> operator, as it does what it does. It would sort of be like asking to fix <code>+</code> to only add the 1's place. Just not going to happen.</p> <p>You can achieve the desired operation, but you need a bit more "stuff" to get it going. First you must have <em>something</em> (another int) that specifies the <em>bits of interest</em>. This is typically called a <strong>bit mask</strong>.</p> <pre><code> int mask = 0x00000007; // just the last 3 bits. int masked_inverse = (~value) &amp; mask; </code></pre> <p>Note that what we did was really invert 32 bits, then zeroed out 29 of those bits; because, they were set to zero in the mask, which means "we don't care about them". This can also be imagined as leveraging the <code>&amp;</code> operator such that we say "if set and we care about it, set it".</p> <p>Now you will <em>still</em> have 32 bits, but only the lower 3 will be inverted. If you want a 3 bit data structure, then that's a different story. Java (and most languages) just don't support such things directly. So, you might be tempted to add another <em>type</em> to Java to support that. Java adds types via a <em>class</em> mechanism, but the built-in types are not changeable. This means you could write a class to represent a 3 bit data structure, but it will have to handle ints internally as 32 bit fields.</p> <p>Fortunately for you, someone has already done this. It is part of the standard Java library, and <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/BitSet.html">is called a <code>BitSet</code></a>. </p> <pre><code>BitSet threeBits = new BitSet(3); threeBits.set(2); // set bit index 2 threeBits.set(0); // set bit index 0 threeBits.flip(0,3); </code></pre> <p>However, such bit manipulations have a different feel to them due to the constraints of the Class / Object system in Java, which follows from defining classes as the <em>only way</em> to add new types in Java.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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