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.
How to set border on jPanel?
J2ME GameCanvas recognizing keys
JUNIT Test class in Eclipse - java.lang.ClassNotFoundException
Not able to append XML element in response
Maps.uniqueIndex() for Multimap?
If server port is wrong, the client is waiting for response
Is it not possible to add text to a single JTextArea dynamically?
reading from DataInputStream without knowing the output type
Niocchi crawler - how to add url to crawle during crawling process (crawling whole website)
Java profiling: detect which piece of code causes high CPU load
JPQL/HSQL update with a limit?
Garbage Collection Details: Is this object eligible for GC?
need to pass generic class in reflection get method
JTA transaction unexpectedly rolled back, nested exception is javax.transaction.RollbackException
How to play songs using XML in my Android application?
problem in setString method.in retrieving data from the table