
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Reverse actual bits of the given number in Java
Given an integer n that is not negative. The goal is to reverse the bits of n and report the number that results from doing so. While reversing the bits, the actual binary form of the integer is used; no leading 0s are taken into account.
Let us see various input output scenarios for this
Input − 13
Output − Reverse actual bits of the given number 11
(13)10 = (1101)2. After reversing the bits, we get: (1011)2 = (11)10.
Explanation − The binary bits are obtained from the input number which is then reversed and finally converted to decimal format which is returned as output.
Input − 18
Output − Reverse actual bits of the given number 9
(18)10 = (10010)2. After reversing the bits, we get: (1001)2 = (9)10.
Explanation −The binary bits are obtained from the input number which is then reversed and finally converted to decimal format which is returned as output.
Approach used in the below program is as follows
Inside the main method
The number is taken input and passed in the method reverseBinaryBits(int input)
Inside the method reverseBinaryBits(int input)
A variable rev_input is initialized to store reversed bits
A loop is iterated with breaking point(input > 0)(we are traversing from the right)
The bitwise right shift operation is used to retrieve one by one bits in the binary representation of n, and the bitwise left shift operation is used to accumulate them in rev.
Example
class TutorialsPoint{ public static int reverseBinaryBits(int input){ int rev_input = 0; while (input > 0){ rev_input <<= 1; if ((int) (input & 1) == 1){ rev_input ^= 1; } input >>= 1; } return rev_input; } public static void main(String[] args){ int input = 13; System.out.println("Reverse actual bits of the given number"); System.out.println(reverseBinaryBits(input)); } }
Output
If we run the above code it will generate the following Output
Reverse actual bits of the given number 11
- Related Articles
- Java program to reverse bits of a positive integer number
- JavaScript Reverse the order of the bits in a given integer
- Java program to print the reverse of the given number
- Reverse Bits in C++
- Python program to reverse bits of a positive integer number?
- Write an Efficient C Program to Reverse Bits of a Number in C++
- Minimum number using set bits of a given number in C++
- C program to rotate the bits for a given number
- Reverse words in a given String in Java
- How to find the Reverse of a given number using Recursion in Golang?
- How to reverse a given string in Java?
- Count number of bits changed after adding 1 to given N in C++
- Java program to count total bits in a number
- Swift program to find the reverse of a given number using recursion
- Number of 1 Bits in Python
