ADVERTISEMENT
ADVERTISEMENT

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

Example of the right shift (>>) operator: Given a value, we have to perform right shift operation.
Submitted by Nidhi, on March 07, 2022

Problem Solution:

Right Shift Operator (>>) is a bitwise operator, which operates on bits. It is used to shift a given number of bits in the right and inserts 0's in the right.

In this program, we will demonstrate the use of the right shift (>>) operator and print the result.

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.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.