How can i right shift a binary number and display all of the positions in java ?

Recommended Answers

All 3 Replies

>> - shift right. i.e. 20 >> 2 = 5
<< - shift left. i.e. 5<<2 = 20
>>> - unsigned shift right.

You can read it all in Wikipedia

One more thing - you don't have to use the bitwise shifting operators in order to complete your task - shifting to the right by 1 is actually dividing by 2, and java does not care how you represent your number. Hex, Binary, Octal, a number is a number. This code should do the trick:

public static void shiftNumber(int n)
{
  while(n != 0)
  {
    System.out.println(Integer.toBinaryString(n));
    n = n >> 1; //equivilant to n=n/2;
  }
}

You may read this for more tips.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.