java


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.

Related Links

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

Categories

HOME
ibm-bluemix
google-chrome-extension
amazon-ec2
oracle11g
relayjs
sd-card
amazon-ecs
jpeg
cross-browser
fingerprint
onelogin
networkx
mouse
spring-tool-suite
remote-access
size
openrefine
aurigma
reactcsstransitiongroup
django-cms
nodatime
kryo
clickonce
angular2-aot
xpages-ssjs
one-to-many
objectanimator
primitive
quote
nat
microsoft-chart-controls
media-queries
exuberant-ctags
fog
caret
azure-sql-database
bootstrapper
tasker
preconditions
galen
pdf-reactor
elmah
janrain
glew
cubic-spline
azure-application-gateway
chain-builder
clean-architecture
fancybox-2
tizen-native-app
boost-preprocessor
django-scheduler
deadbolt-2
underscore.js-templating
qpid
firebaseui
persist
ready-api
essence
pickadate
hexdump
dataview
bluegiga
pagerank
jms2
marching-cubes
angular-leaflet-directive
splash
operation
angular-local-storage
truevault
apache-commons-net
dia
dd
network-interface
google-style-guide
file-copying
mdt
nsbutton
typeof
mysql-error-1062
transcoding
pyhdf
cos
qt-faststart
random-seed
mqx
bitsharp
funscript
manchester-syntax
javaspaces
code-cleanup
window-management
zpt
hirefire
coda-slider
suppress
adrotator
port-number
uiq3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App