• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Doubts on shift operations

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class TEST1
{
public static void main(String[] args) {
byte x=3, y=5;
System.out.println((-x == ~x+1) + ","+(-y == ~y+1));
}
}


class TEST2
{
public static void main(String[] args) {
byte x=127;
x<<=2;
System.out.println(x);
}
}



class TEST3
{
public static void main(String[] args) {
byte y=5;
System.out.println(y<<33);
}
}

Can any one help out in solving the shift operations(<<,>>,<<< ?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What question do you have?
 
Kiran Chand Panga
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you tell me how to solve these three Problems (which I posted earlier)?
[ March 01, 2006: Message edited by: Kiran Chand Panga ]
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kiran Chand Panga:
class TEST1
{
public static void main(String[] args) {
byte x=3, y=5;
System.out.println((-x == ~x+1) + ","+(-y == ~y+1));
}
}



This is simply testing that in order to form the negative of a number you flip the bits and add 1.

class TEST2
{
public static void main(String[] args) {
byte x=127;
x<<=2;
System.out.println(x);
}
}



Basically this is shifting 2 0s onto the end of the bits 01111111. When you do this you get 11111100. This is -4.

class TEST3
{
public static void main(String[] args) {
byte y=5;
System.out.println(y<<33);
}
}



Before the shift operation is carried out, we mod 33 by 32. So we are shifting 1 bit.

So we get 10.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CONDITIONS:

ONLY VALID FOR NON NEGATIVE NOS

in most cases it will work except for cases in which final result using this becomes greater than pow(2,31)-1:


1) in case of << :
suppose expression is a<<b
answer will be a*pow(2,b)

2) in case of >> or >>>
final expression::
a/pow(2,b)

BUT DO REMEMBER THE ABOVE CONDITIONS . IF "a" DOES NOT SATISFY THE CONDITIONS DONT USE THIS TECHNIQUE
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember sports fans - shift operators are on the 1.4 exam, but they are NOT on the 1.5 exam!
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic