
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Binary representation of next number in C++
In this problem, we are given the binary representation of a number and we have to find the binary representation of the next number i.e. the number that is resulted after adding one to the given number.
Binary representation of a number is changing the base of the number to base 2 and representing the number using only 0 or 1.
For example, Binary representation of 14 is 1110.
So, here we would be given a number, let's say n in binary form. And we have to find the binary representation of n+1.
To solve this problem, we need to know the basics of binary addition. Let's see what happens when 1 is added to 0 or 1 in binary form.
0 + 1 = 1
1 + 1 = 10
Example
Let's see an example on how to solve the above problem,
Input: 010010111 Output: 010011000 Explanation : (010010111)2 is the binary representation of 152 and the next number will be 153 whose binary representation is (010011000)2. We will use binary addition here and add binary (1)2 to the binary representation of the number.
From the above example we can see that on adding binary 1 to the number all the ones starting from right are converted to 0, until the first 0 is encountered and this 0 is flipped to 1. now let's create an algorithm for this logic.
Algorithm
Step 1 : Start right to left i.e n-1 to 0 Step 2 : If 0 is encountered, change it 1 and break Step 3 : If one is encounter change it to 0. Step 4 : When no zero is encountered, add 1 to the start of the array. Step 5 : Print the array.
Example
Now, let's see the code implementation of this algorithm.
#include <bits/stdc++.h> using namespace std; string nextBinary(string num) { int l = num.size(); int flag = 0 ; for (int i=l-1; i>=0; i--) { if (num.at(i) == '0') { num.at(i) = '1'; flag = 1; break; } else num.at(i) = '0'; } if (flag < 0) num = "1" + num; return num; } int main() { string number = "0111010111"; cout<<"The Binary representation of the number is "<<number<<endl; cout<<"Binary representation of next number is "<<nextBinary(number); return 0; }
Output
The Binary representation of the number is 0111010111 Binary representation of next number is 0111011000
- Related Articles
- Binary representation of next greater number with same number of 1’s and 0’s in C Program?
- Binary representation of previous number in C++
- Binary representation of a given number in C++
- Prime Number of Set Bits in Binary Representation in C++
- Prime Number of Set Bits in Binary Representation in Python
- Number of leading zeros in binary representation of a given number in C++
- Check if binary representation of a number is palindrome in Python
- Sorting according to number of 1s in binary representation using JavaScript
- Number of Steps to Reduce a Number in Binary Representation to One in C++
- Count number of trailing zeros in Binary representation of a number using Bitset in C++
- Array Representation Of Binary Heap
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Binary Tree Representation in Data Structures
- Check if the binary representation of a number has equal number of 0s and 1s in blocks in Python
- Calculating 1s in binary representation of numbers in JavaScript
