• 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

Unable to Understand the left shift operator

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I am having trouble understanding << ,
i am running the following code

int i = -13;

int result = 0;
result = 1<< 1;

Result is displayed as
11111111111111111111111111110011

could anyonle please explain how thisis happening?

Thanks in advance
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, AnuragSudha!

First of all, I think you wrote wrong your code.
This must have been your intention:


The result is: 11111111111111111111111111100110;

Now, here is the explination:
i = -13, its represantation: 11111111111111111111111111110011
i << 1, means add 1 zero starting with the lowest bit:
11111111111111111111111111100110 (= -26).

By the way, x<<n means x*2^n (x mutiplied with 2 at the power n).
In this case -13<<1 = -13*2^1 = -13*2 = -26.
 
AnuragSudha Jain
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Emilia ,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic