• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mughal Q3.14

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic