Does a logical right shift ever add a 1 in Java?
I have a paper that has 1111 1111 1111 xxxx as the answer to (short)(Z>>>12). The program given to us to check our bit operation answers also gives this answer to a (short) input. From my understanding, only the arithmetic shift (>>) is sign sensitive. Edit: Z = -2009 (or 1111100000100111 in binary) (short)(Z>>>12) = -1 (or 1111111111111111 in binary)
As I understand the problem, it may be a trick question. Here is one such program that outputs the given result: short z = (short)0b1111_1000_0010_0111; // -2009 System.out.println( (short)(z >>> 12) ); // prints -1 This happens because before the right-shift, the short gets promoted to int by way of sign extension. This fills the 16 bits above the range of short with 1s. So to put it another way: // as a short 1111_1000_0010_0111 // sign extended to int 1111_1111_1111_1111_1111_1000_0010_0111 // shifted right 12 0000_0000_0000_1111_1111_1111_1111_1111 // cast back to short 1111_1111_1111_1111 If you don't want the sign extension, you have to mask: System.out.println( (short)((z & 0xffff) >>> 12) ); Mathematical operators in Java do not ever operate on char, byte and short, they always get implicitly promoted to int first.
Problems with Android Location class [duplicate]
what is the time difference between loading data into PDF file and excel file using java?
After changing log schema, zeppelin demo Java app giving compile error & referring to old schema class
How do I refer to a field in the class which I am currently defining with Byte Buddy?
Running code taking undefined number of inputs in Netbeans
android - SkImageDecoder::Factory returned null
Perlin Noise is not Gaining Depth with More Octaves
Clicking on <li> list item not using Selenium Java
Regex exclude lines not containing words but some words
Having application as standalone as well as dependency in Spring Boot 1.5
KeyListener on panel in java Swing. Wrong output
Spring Boot Test: Is it a good idea to have Application class per module?
waits until the program is running
Java list only addition no deletion
Write and read data from Arduino using serial com in Java
Read mathematics operation input in Java