- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

**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.

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.

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)); } }

If we run the above code it will generate the following Output

Reverse actual bits of the given number 11

- Related Questions & Answers
- JavaScript Reverse the order of the bits in a given integer
- Java program to reverse bits of a positive integer number
- Java program to print the reverse of the given number
- Reverse Bits in C++
- Python program to reverse bits of a positive integer number?
- Minimum number using set bits of a given number in C++
- Write an Efficient C Program to Reverse Bits of a Number in C++
- C program to rotate the bits for a given number
- Set the capacity to the actual number of elements in the ArrayList in C#?
- Number of 1 Bits in Python
- Setting the capacity to the actual number of elements in a SortedList object in C#?
- Count number of bits changed after adding 1 to given N in C++
- Reverse words in a given String in Java
- How to reverse a given string in Java?
- Java program to reverse an array in groups of given size

Advertisements