• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mughal Q3.14  RSS feed

 
Prosenjit Banerjee
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?
 
Jessica Sant
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!