Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Mughal Q3.14

Ranch Hand
Posts: 105
In Khalid Mughal's book question no. 3.14 on the page 73 goes like this:
Given a variable x of type int (which may contain a negative value), which are correct ways of doubling the value of x?
(a) x << 1 ;
(b) x = x * 2 ;
(c) x *= 2 ;
(d) x += x ;
(e) x <<= 1 ;

The correct answer given in the book is b, c, d and e.
b, c and d are alright. But will e allso be valid? Suppose, x is a negative number. will x <<= 1 really make it double?

Sheriff
Posts: 4313
try running it in a lil java class -->

output:
C:\blah>java doubleIt
negative = -11
positive = 13
negative <<= 1 : -22
positive <<= 1 : 26

Prosenjit Banerjee
Ranch Hand
Posts: 105
Mughal Q3.14
------------
Thanks Jessica Sant. I din't have the picture of -ve patterns in mind at
that time. I still remember how beautiful the bit pattern of -ve numbers
are! They are realy cleverly made.

 I found a beautiful pie. And a tiny ad: Why should you try IntelliJ IDEA ? https://coderanch.com/wiki/696337/IntelliJ-IDEA