• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Mughal Q3.14  RSS feed

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Select al valid answers.
(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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!