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

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

Categories

HOME
ms-access
ionic-framework
proxy
urbancode
view
relayjs
sd-card
xmpp
sharepoint-designer
cakephp-2.5
v8
win32gui
baqend
ojdbc
upload
qore
nano-server
izpack
imacros
windows-phone-7
ups
netflix
django-simple-history
after-effects
graphlab
transformation
normalizr
textfield
nodatime
mps
telerik-reporting
format-specifiers
jndi
emgucv
xpages-ssjs
social-media
elasticsearch-net
large-file-upload
vision
c++-amp
google-rich-snippets
ecto
google-cloud-endpoints-v2
vsts-build-task
force-layout
main
hot-module-replacement
starteam
mapzen
estimote
thin
upstart
revapi
jedis
paxos
winscp-net
svmlight
carrot
windows-mobile-6.5
pearson
imanage
iso8601
minimization
eventkit
root-framework
word-vba-mac
abcpdf9
vmware-tools
fody
etsy
bgp
livequery
pagerank
asp.net5
associative-array
netmq
javax.sound.midi
srand
endeca-workbench
responsive-images
twirl
bigbluebutton
sorl-thumbnail
tween
google-style-guide
cdt
java.util.concurrent
googlemock
dml
phpthumb
undefined-reference
oracle-warehouse-builder
flexmojos
pyhdf
floating-point-precision
odata4j
shellexecute
mqx
selected
anonymous-methods
multipage
hgsubversion
transactionscope
boost-filesystem
xdomainrequest
xtype
yetanotherforum
krl
propagation
createwindow
gamma
user-friendly
substrings
mtj
simpletest
aquaticprime

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App