• 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

Shift Operator

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to get clear concept in Shift operators.
I have seen the previous forums.I was not able to understand
about << for -ve numbers.Ex: how would -3<<2 give -12
Anyone please answer in detail along with steps,since i am trying to get these concepts from many days.
Thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You woul dneed to put the operands in the binary format and then it would be very easy to understand the operations of shifting on them.
Thanks,
Jatinder.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
-3 is 1111 1111 1111 1111 1111 1111 1111 1101
0th bit is 1, 2nd bit is -4 which makes it (-4 +1= -3)
As << fills zeros from the least significant bit (R.H.S)
-3 << 2 is 1111 1111 1111 1111 1111 1111 1111 0100 which is -12
as 4th bit is -16 & 2nd bit is 4
i.e, -16 + 4 = 12.
Hope it helps.
KN
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! for the Help.I am clear regarding int.
But, i have another doubt regarding char values.
If char c='1';
System.out.println(c>>1);
o/p:49(decimal value).Can anyone explain this to me.
And if i give char c='10'; Why do i get
a compile-error.Please anyone clarify
my doubt.

[This message has been edited by avn (edited July 21, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone please clarify my doubt!!!
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi avn
char refers to characters not to strings. Please have this very clear.
a, b , c etc are characters and "abc" etc are strings
when u say char c='10' u r trying to assign to a character a string. this cannot be done
regds.
Rahul
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!Rahul.I get it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic