Q:

Java program to demonstrate the example of the right shift (>>) operator

belongs to collection: Java Basic Programs

0

Java program to demonstrate the example of the right shift (>>) operator

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to demonstrate an example of the left shift (<<) operator is given below. The given program is compiled and executed successfully.

// Java program to demonstrate an example of 
// the right shift (>>) operator

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    int num = 0xff;

    System.out.printf("Number before right shift: %04X\n", num);

    //shifting 2 bits right
    num = (num >> 2);

    System.out.printf("Number after right shift: %04X\n", num);
  }
}

Output:

Number before right shift: 00FF
Number after right shift: 003F

Explanation:

In the above program, we created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we created an integer variable initialized with 0xff. Then we performed the right shift operation and printed the result.

Binary of 0xFF in (in bytes format) - 0000 0000 1111 1111.

After 2 bits right shift (in bytes format) – 0000 0000 0011 1111, which is equivalent to 0x003F.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to SET and CLEAR bits of the given nu... >>
<< Java program to demonstrate the example of left sh...