Bits manipulation (Important tactics)
Last Updated :
07 May, 2024
Prerequisites: Bitwise operators in C, Bitwise Hacks for Competitive Programming, Bit Tricks for Competitive Programming
Table of Contents
1. Compute XOR from 1 to n (direct method):
The  problem can be solved based on the following observations:
Say x = n%4. The XOR value depends on the value if x. If
- x = 0, then the answer is n.
- x = 1, then answer is 1.
- x = 2, then answer is n+1.
- x = 3, then answer is 0.
Below is the implementation of the above approach.
CPP
// Direct XOR of all numbers from 1 to n
int computeXOR(int n)
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
else
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
// Direct XOR of all numbers from 1 to n
public static int computeXOR(int n)
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
else
return 0;
}
public static void main (String[] args) {
}
}
// This code is contributed by akashish__
Python
# Direct XOR of all numbers from 1 to n
def computeXOR(n):
if (n % 4 is 0):
return n
if (n % 4 is 1):
return 1
if (n % 4 is 2):
return n + 1
else:
return 0
# This code is contributed by akashish__
C#
using System;
public class GFG
{
// Direct XOR of all numbers from 1 to n
public static int computeXOR(int n)
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
else
return 0;
}
public static void Main(){}
}
// This code is contributed by akashish__
JavaScript
<script>
// Direct XOR of all numbers from 1 to n
function computeXOR(n)
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
else
return 0;
}
// This code is contributed by Shubham Singh
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Refer Compute XOR from 1 to n for details.
2. Count of numbers (x) smaller than or equal to n such that n+x = n^x:
The count of such numbers x can be counted using the following mathematical trick.Â
The count = pow(2, count of zero bits).
C++
// Count of numbers (x) smaller than or equal to n such that n+x = n^x:
// here unset bits means zero bits
#include <bits/stdc++.h>
using namespace std;
// function to count number of values less than
// equal to n that satisfy the given condition
int countValues(int n)
{
// unset_bits keeps track of count of un-set
// bits in binary representation of n
int unset_bits=0;
while (n)
{
if ((n & 1) == 0)
unset_bits++;
n=n>>1;
}
// Return 2 ^ unset_bits i.e. pow(2,count of zero bits)
return 1 << unset_bits;
}
// Driver code
int main()
{
int n = 15;
cout << countValues(n);
return 0;
}
// contributed by akashish__
Java
// Count of numbers (x) smaller than or equal to n such that n+x = n^x:
// here unset bits means zero bits
import java.io.*;
class GFG
{
// function to count number of values less than
// equal to n that satisfy the given condition
public static int countValues(int n)
{
// unset_bits keeps track of count of un-set
// bits in binary representation of n
int unset_bits = 0;
while (n > 0)
{
if ((n & 1) == 0)
unset_bits++;
n = n>>1;
}
// Return 2 ^ unset_bits i.e. pow(2,count of zero bits)
return 1 << unset_bits;
}
// Driver code
public static void main (String[] args) {
int n = 15;
System.out.print(countValues(n));
}
}
// This code is contributed by poojaagrawal2.
Python
# Count of numbers (x) smaller than or equal to n such that n+x = n^x:
# here unset bits means zero bits
# function to count number of values less than
# equal to n that satisfy the given condition
def countValues(n):
# unset_bits keeps track of count of un-set
# bits in binary representation of n
unset_bits=0
while (n):
if ((n & 1) == 0):
unset_bits+=1
n=n>>1
# Return 2 ^ unset_bits i.e. pow(2,count of zero bits)
return 1 << unset_bits
# Driver code
n = 15
print(countValues(n))
# This code is contributed by akashish__
C#
// C# code to implement the approach
using System;
using System.Collections.Generic;
class GFG {
// Count of numbers (x) smaller than or equal to n such that n+x = n^x:
// here unset bits means zero bits
// function to count number of values less than
// equal to n that satisfy the given condition
static int countValues(int n)
{
// unset_bits keeps track of count of un-set
// bits in binary representation of n
int unset_bits = 0;
while (n > 0)
{
if ((n & 1) == 0)
unset_bits++;
n = n>>1;
}
// Return 2 ^ unset_bits i.e. pow(2,count of zero bits)
return 1 << unset_bits;
}
// Driver code
public static void Main()
{
int n = 15;
Console.Write(countValues(n));
}
}
// This code is contributed by agrawalpoojaa976.
JavaScript
// Count of numbers (x) smaller than or equal to n such that n+x = n^x:
// here unset bits means zero bits
// function to count number of values less than
// equal to n that satisfy the given condition
function countValues(n)
{
// unset_bits keeps track of count of un-set
// bits in binary representation of n
let unset_bits=0;
while (n)
{
if ((n & 1) == 0)
unset_bits++;
n=n>>1;
}
// Return 2 ^ unset_bits i.e. pow(2,count of zero bits)
return 1 << unset_bits;
}
// Driver code
let n = 15;
document.write(countValues(n));
Refer Equal Sum and XOR for details.
Time complexity: O(log n)
Auxiliary Space: O(1)
3. How to know if a number is a power of 2?
This can be solved based on the following fact:
If a number N is a power of 2, then the bitwise AND of N and N-1 will be 0. But this will not work if N is 0. So just check these two conditions, if any of these two conditions is true.
Refer check if a number is power of two for details.
Below is the implementation of the above approach.
CPP
// Function to check if x is power of 2
bool isPowerOfTwo(int x)
{
// First x in the below expression is
// for the case when x is 0
return x && (!(x & (x - 1)));
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Function to check if x is power of 2
static public boolean isPowerOfTwo(int x)
{
// First x in the below expression is
// for the case when x is 0
return (x != 0) && ((x & (x - 1)) == 0);
}
public static void main (String[] args) {
}
}
// contributed by akashish__
Python
# Function to check if x is power of 2
def isPowerOfTwo(x):
# First x in the below expression is
# for the case when x is 0
return x and (not(x & (x - 1)))
# This code is contributed by akashish__
C#
using System;
public class GFG{
// Function to check if x is power of 2
static public bool isPowerOfTwo(int x)
{
// First x in the below expression is
// for the case when x is 0
return (x != 0) && ((x & (x - 1)) == 0);
}
static public void Main (){
}
}
// This code is contributed by akashish__
JavaScript
// Function to check if x is power of 2
function isPowerOfTwo(x)
{
// First x in the below expression is
// for the case when x is 0
return x && (!(x & (x - 1)));
}
// This code is contributed by akashish__
Time Complexity: O(1)
Auxiliary Space: O(1)
4. Find XOR of all subsets of a set
We can do it in O(1) time. The answer is always 0 if the given set has more than one element. For sets with a single element, the answer is the value of the single element.Â
Refer XOR of the XORâs of all subsets for details.
5. Find the number of leading, trailing zeroes and number of 1âs
We can quickly find the number of leading, trailing zeroes and number of 1âs in a binary code of an integer in C++ using GCC.Â
It can be done by using inbuilt functions i.e.
Number of leading zeroes: __builtin_clz(x)
Number of trailing zeroes : __builtin_ctz(x)
Number of 1-bits: __builtin_popcount(x)Â
Refer GCC inbuilt functions for details.
6. Convert binary code directly into an integer in C++
CPP
// Conversion into Binary code
#include <iostream>
using namespace std;
int main()
{
auto number = 0b011;
cout << number;
return 0;
}
Java
/*package whatever //do not write package name here */
// Conversion into Binary code
import java.io.*;
class GFG {
public static void main(String[] args)
{
int number = 0b011;
System.out.println(number);
}
}
// This code is contributed by akashish__
Python
# Python Code
number = 0b011
print(number)
# This code is contributed by akashish__
C#
// Conversion into Binary code
using System;
public class GFG {
static public void Main()
{
// Code
int number = 0b011;
Console.WriteLine(number);
}
}
// This code is contributed by karthik
JavaScript
// Conversion into Binary code
let number = 0b011;
console.log(number);
Time Complexity: O(1)
Auxiliary Space: O(1)
7. The Quickest way to swap two numbers:
Two numbers can be swapped easily using the following bitwise operations:
a ^= b;
b ^= a;Â
a ^= b;
C++
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 7;
cout<<"Before Swapping, a = "<<a<<" "<<"b = "<<b<<endl;
a ^= b;
b ^= a;
a ^= b;
cout<<"After Swapping, a = "<<a<<" "<<"b = "<<b<<endl;
return 0;
}
// This code is contributed by akashish__
Java
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int a = 5;
int b = 7;
System.out.print("Before Swapping, a = ");
System.out.print(a);
System.out.print(" ");
System.out.print("b = ");
System.out.print(b);
System.out.println("");
a ^= b;
b ^= a;
a ^= b;
System.out.print("After Swapping, a = ");
System.out.print(a);
System.out.print(" ");
System.out.print("b = ");
System.out.print(b);
}
}
// This code is contributed by akashish__
Python
a = 5
b = 7
print("Before Swapping, a = ",a," ","b = ",b)
a ^= b
b ^= a
a ^= b
print("After Swapping, a = ",a," ","b = ",b)
# This code is contributed by akashish__
C#
using System;
public class GFG {
static public void Main()
{
int a = 5;
int b = 7;
Console.WriteLine("Before Swapping, a = " + a + " "
+ "b = " + b);
a ^= b;
b ^= a;
a ^= b;
Console.WriteLine("After Swapping, a = " + a + " "
+ "b = " + b);
}
}
// This code is contributed by akashish__
JavaScript
let a = 5;
let b = 7;
console.log("Before Swapping, a = " , a , " " , "b = " , b);
a ^= b;
b ^= a;
a ^= b;
console.log("After Swapping, a = " , a , " " , "b = " , b);
// This code is contributed by akashish__
OutputBefore Swapping, a = 5 b = 7
After Swapping, a = 7 b = 5
Time Complexity: O(1)
Auxiliary Space: O(1)
Refer swap two numbers for more details. Â
8. Finding the most significant set bit (MSB):
We can find the most significant set bit in O(1) time for a fixed size integer. For example below code is for 32-bit integer. Â
C++
int setBitNumber(int n)
{
// Below steps set bits after
// MSB (including MSB)
// Suppose n is 273 (binary
// is 100010001). It does following
// 100010001 | 010001000 = 110011001
n |= n >> 1;
// This makes sure 4 bits
// (From MSB and including MSB)
// are set. It does following
// 110011001 | 001100110 = 111111111
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
// Increment n by 1 so that
// there is only one set bit
// which is just before original
// MSB. n now becomes 1000000000
n = n + 1;
// Return original MSB after shifting.
// n now becomes 100000000
return (n >> 1);
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static int setBitNumber(int n)
{
// Below steps set bits after
// MSB (including MSB)
// Suppose n is 273 (binary
// is 100010001). It does following
// 100010001 | 010001000 = 110011001
n |= n >> 1;
// This makes sure 4 bits
// (From MSB and including MSB)
// are set. It does following
// 110011001 | 001100110 = 111111111
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
// Increment n by 1 so that
// there is only one set bit
// which is just before original
// MSB. n now becomes 1000000000
n = n + 1;
// Return original MSB after shifting.
// n now becomes 100000000
return (n >> 1);
}
public static void main (String[] args) {
}
}
// This code is contributed by akashish__
Python
def setBitNumber(n):
# Below steps set bits after
# MSB (including MSB)
# Suppose n is 273 (binary
# is 100010001). It does following
# 100010001 | 010001000 = 110011001
n |= n >> 1
# This makes sure 4 bits
# (From MSB and including MSB)
# are set. It does following
# 110011001 | 001100110 = 111111111
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
# Increment n by 1 so that
# there is only one set bit
# which is just before original
# MSB. n now becomes 1000000000
n = n + 1
# Return original MSB after shifting.
# n now becomes 100000000
return (n >> 1)
# This code is contributed by akashish__
C#
using System;
public class GFG{
public static int setBitNumber(int n)
{
// Below steps set bits after
// MSB (including MSB)
// Suppose n is 273 (binary
// is 100010001). It does following
// 100010001 | 010001000 = 110011001
n |= n >> 1;
// This makes sure 4 bits
// (From MSB and including MSB)
// are set. It does following
// 110011001 | 001100110 = 111111111
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
// Increment n by 1 so that
// there is only one set bit
// which is just before original
// MSB. n now becomes 1000000000
n = n + 1;
// Return original MSB after shifting.
// n now becomes 100000000
return (n >> 1);
}
static public void Main (){
// Code
}
}
// This code is contributed by akashish__
JavaScript
function setBitNumber(n)
{
// Below steps set bits after
// MSB (including MSB)
// Suppose n is 273 (binary
// is 100010001). It does following
// 100010001 | 010001000 = 110011001
n |= n >> 1;
// This makes sure 4 bits
// (From MSB and including MSB)
// are set. It does following
// 110011001 | 001100110 = 111111111
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
// Increment n by 1 so that
// there is only one set bit
// which is just before original
// MSB. n now becomes 1000000000
n = n + 1;
// Return original MSB after shifting.
// n now becomes 100000000
return (n >> 1);
}
// This code is contributed by akashish__
Time Complexity: O(1)
Auxiliary Space: O(1)
Refer Find most significant set bit of a number for details.Â
9. Check if a number has bits in an alternate pattern
We can quickly check if bits in a number are in an alternate pattern (like 101010).Â
Compute bitwise XOR (XOR denoted using ^) of n and (n >> 1). If n has an alternate pattern, then n ^ (n >> 1) operation will produce a number having all bits set.
Below is the implementation of the above approach.
C++
// function to check if all the bits
// are set or not in the binary
// representation of 'n'
static bool allBitsAreSet(int n)
{
// if true, then all bits are set
if (((n + 1) & n) == 0)
return true;
// else all bits are not set
return false;
}
// Function to check if a number
// has bits in alternate pattern
bool bitsAreInAltOrder(unsigned int n)
{
unsigned int num = n ^ (n >> 1);
// To check if all bits are set in 'num'
return allBitsAreSet(num);
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// function to check if all the bits
// are set or not in the binary
// representation of 'n'
public static boolean allBitsAreSet(long n)
{
// if true, then all bits are set
if (((n + 1) & n) == 0)
return true;
// else all bits are not set
return false;
}
// Function to check if a number
// has bits in alternate pattern
public static boolean bitsAreInAltOrder(long n)
{
long num = n ^ (n >> 1);
// To check if all bits are set in 'num'
return allBitsAreSet(num);
}
public static void main (String[] args) {
}
}
// This code is contributed by akashish__
Python
# function to check if all the bits
# are set or not in the binary
# representation of 'n'
def allBitsAreSet(n):
# if true, then all bits are set
if (((n + 1) & n) == 0):
return True
# else all bits are not set
return False
# Function to check if a number
# has bits in alternate pattern
def bitsAreInAltOrder(n):
num = n ^ (n >> 1)
# To check if all bits are set in 'num'
return allBitsAreSet(num)
# This code is contributed by akashish__
C#
using System;
public class GFG
{
// function to check if all the bits
// are set or not in the binary
// representation of 'n'
public static bool allBitsAreSet(uint n)
{
// if true, then all bits are set
if (((n + 1) & n) == 0)
return true;
// else all bits are not set
return false;
}
// Function to check if a number
// has bits in alternate pattern
public static bool bitsAreInAltOrder(uint n)
{
uint num = n ^ (n >> 1);
// To check if all bits are set in 'num'
return allBitsAreSet(num);
}
static public void Main() {}
}
// This code is contributed by akashish__
JavaScript
// function to check if all the bits
// are set or not in the binary
// representation of 'n'
function allBitsAreSet(n)
{
// if true, then all bits are set
if (((n + 1) & n) == 0)
return true;
// else all bits are not set
return false;
}
// Function to check if a number
// has bits in alternate pattern
function bitsAreInAltOrder(n)
{
let num = n ^ (n >> 1);
// To check if all bits are set in 'num'
return allBitsAreSet(num);
}
// This code is contributed by akashish__
Time Complexity: O(1)
Auxiliary Space: O(1)
Refer check if a number has bits in alternate pattern for details.

DSA Self Paced Course
Similar Reads
Bitwise Algorithms
Bitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Sift, and Right Shift. BasicsIntroduction to Bitwise Algorit
4 min read
Introduction to Bitwise Algorithms - Data Structures and Algorithms Tutorial
Bit stands for binary digit. A bit is the basic unit of information and can only have one of two possible values that is 0 or 1. In our world, we usually with numbers using the decimal base. In other words. we use the digit 0 to 9 However, there are other number representations that can be quite use
15+ min read
Bitwise Operators in C
In C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C. The & (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if bot
7 min read
Bitwise Operators in Java
Operators constitute the basic building block of any programming language. Java, too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
6 min read
Python Bitwise Operators
Python bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on each bit or corresponding pair of bits, hence the name bitwise operators. The result is then returned in decimal format. Note: Python bitwi
6 min read
JavaScript Bitwise Operators
In JavaScript, a number is stored as a 64-bit floating-point number but bitwise operations are performed on a 32-bit binary number. To perform a bit-operation, JavaScript converts the number into a 32-bit binary number (signed) and performs the operation and converts back the result to a 64-bit numb
5 min read
All about Bit Manipulation
Bit Manipulation is a technique used in a variety of problems to get the solution in an optimized way. This technique is very effective from a Competitive Programming point of view. It is all about Bitwise Operators which directly works upon binary numbers or bits of numbers that help the implementa
14 min read
What is Endianness? Big-Endian & Little-Endian
Computers operate using binary code, a language made up of 0s and 1s. This binary code forms the foundation of all computer operations, enabling everything from rendering videos to processing complex algorithms. A single bit is a 0 or a 1, and eight bits make up a byte. While some data, such as cert
5 min read
Bits manipulation (Important tactics)
Prerequisites: Bitwise operators in C, Bitwise Hacks for Competitive Programming, Bit Tricks for Competitive Programming Table of Contents Compute XOR from 1 to n (direct method)Count of numbers (x) smaller than or equal to n such that n+x = n^xHow to know if a number is a power of 2?Find XOR of all
15+ min read
Easy Problems on Bit Manipulations and Bitwise Algorithms
Binary representation of a given number
Given an integer n, the task is to print the binary representation of the number. Note: The given number will be maximum of 32 bits, so append 0's to the left if the result string is smaller than 30 length. Examples: Input: n = 2Output: 00000000000000000000000000000010 Input: n = 0Output: 0000000000
6 min read
Count set bits in an integer
Write an efficient program to count the number of 1s in the binary representation of an integer.Examples : Input : n = 6Output : 2Binary representation of 6 is 110 and has 2 set bits Input : n = 13Output : 3Binary representation of 13 is 1101 and has 3 set bits [Naive Approach] - One by One Counting
15+ min read
Add two bit strings
Given two bit sequences as strings, write a function to return the addition of the two sequences. Bit strings can be of different lengths also. For example, if string 1 is "1100011" and second string 2 is "10", then the function should return "1100101". We strongly recommend that you click here and
13 min read
Turn off the rightmost set bit
Given an integer n, turn remove turn off the rightmost set bit in it. Input: 12Output: 8Explanation : Binary representation of 12 is 00...01100. If we turn of the rightmost set bit, we get 00...01000 which is binary representation of 8 Input: 7 Output: 6 Explanation : Binary representation for 7 is
7 min read
Rotate bits of a number
Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end. In left rotation, the bits that fall off at left end are put back at right end. In right rotation, the bits that fall off at right end are put ba
11 min read
Compute modulus division by a power-of-2-number
Given two numbers n and d where d is a power of 2 number, the task is to perform n modulo d without the division and modulo operators. Input: 6 4Output: 2 Explanation: As 6%4 = 2Input: 12 8Output: 4Explanation: As 12%8 = 4Input: 10 2Output: 0Explanation: As 10%2 = 0 Approach: The idea is to leverage
3 min read
Find the Number Occurring Odd Number of Times
Given an array of positive integers. All numbers occur an even number of times except one number which occurs an odd number of times. Find the number in O(n) time & constant space. Examples : Input : arr = {1, 2, 3, 2, 3, 1, 3}Output : 3 Input : arr = {5, 7, 2, 7, 5, 2, 5}Output : 5 Recommended
12 min read
Program to find whether a given number is power of 2
Given a positive integer n, the task is to find if it is a power of 2 or not. Examples: Input : n = 16Output : YesExplanation: 24 = 16 Input : n = 42Output : NoExplanation: 42 is not a power of 2 Input : n = 1Output : YesExplanation: 20 = 1 Approach 1: Using Log - O(1) time and O(1) spaceThe idea is
12 min read
Find position of the only set bit
Given a number N having only one â1â and all other â0âs in its binary representation, find position of the only set bit. If there are 0 or more than 1 set bit the answer should be -1. Position of set bit '1' should be counted starting with 1 from the LSB side in the binary representation of the numb
15+ min read
Check for Integer Overflow
Write a "C" function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum a+b in "result" and returns 0. Otherwise, it returns -1. The solution of casting to long and adding to find detecting the overflow is not allowed.Method 1 There can be overflo
8 min read
Find XOR of two number without using XOR operator
Given two integers, the task is to find XOR of them without using the XOR operator. Examples : Input: x = 1, y = 2Output: 3 Input: x = 3, y = 5Output: 6 Approach - Checking each bit - O(log n) time and O(1) spaceA Simple Solution is to traverse all bits one by one. For every pair of bits, check if b
9 min read
Check if two numbers are equal without using arithmetic and comparison operators
Given two numbers, the task is to check if two numbers are equal without using Arithmetic and Comparison Operators or String functions. Method 1 : The idea is to use XOR operator. XOR of two numbers is 0 if the numbers are the same, otherwise non-zero. C/C++ Code // C++ program to check if two numbe
8 min read
Detect if two integers have opposite signs
Given two integers a and b, the task is to determine whether they have opposite signs. Return true if the signs of the two numbers are different and false otherwise. Examples: Input: a = -5, b = 10Output: trueExplanation: One number is negative and the other is positive, so their signs are different
9 min read
Swap Two Numbers Without Using Third Variable
Given two variables a and y, swap two variables without using a third variable. Examples: Input: a = 2, b = 3Output: a = 3, b = 2 Input: a = 20, b = 0Output: a = 0, b = 20 Input: a = 10, b = 10Output: a = 10, b = 10 Table of Content Using Arithmetic OperatorsUsing Bitwise XORBuilt-in SwapUsing Arith
6 min read
Russian Peasant (Multiply two numbers using bitwise operators)
Given two integers a and b, the task is to multiply them without using the multiplication operator. Instead of that, use the Russian Peasant Algorithm. Examples: Input: a = 2, b = 5Output: 10Explanation: Product of 2 and 5 is 10. Input: a = 6, b = 9Output: 54Explanation: Product of 6 and 9 is 54. In
4 min read