• 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

question abt operator

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given a variable x of type int(can be -ve), 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;
Ans given is:b,c,d,e
I thought it is e because multiplication and addition of -ve number does not result in doubling the result here.
Eg: x=-2;x=-2*2=-4<-2
Thanks in advance
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doubling always means in terms of magnitude.
-2*2=-4
abs(-4) = 4
abs(-2)=2
so in terms of magnitude the value is 4 which is twice the value of abs(-2)
the answer e also does this -2<<=1 will result
in -2*2 rasied to 1 =4
11111110 (8 bits) on left shift by bit introduces o to the right
11111100 which is -4 (so how did u choose e?)
Hope this helps
thank u
Saravanakumar R
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic