Subtract 1 Without Arithmetic Operators


This problem includes that we need to subtract 1 without using arithmetic operators. We will be given any integer N as input in the problem and we need to subtract 1 from the number or simply we need to print N-1. Our task is to perform the operation without using any arithmetic operators.

The arithmetic operations involves variety of operations on numbers like addition(+), subtraction(-), multiplication(*), division(/), modulo(%), etc. These operations are supported by every programming language on numbers. Despite using this we need to subtract 1 from the number.

For example,

Input

7

Output

6

Explanation : The number N entered is 7. Taking away 1 from N yields 6, which is the desired result. Simply keep in mind that we are not permitted to perform any mathematical operations on N.

Input

24

Output

23

Explanation : Subtracting 1 from the number N.

Let’s look at the algorithm that we can use to solve the problem without using any arithmetic operators.

Algorithm

The issue can be resolved without the use of arithmetic operators by using bit manipulation techniques that enable C++'s bitwise operators. The AND operator (and), the OR operator (|), the XOR operator (^), the NOT operator (~), the left shift operator (<<), and the right shift operator. (>>) are among the bitwise operations that can be performed in C. To handle bits in C++, we employ certain fundamental bitwise operations.

To solve the problem, this method merely employs the XOR operator and the left shift operator.

Since any number can be represented in the binary form. The binary representation of 10 will be 1010.

If we observe every number of the form $\mathrm{2^{n}}$, where n can be any positive integer can be represented as:

$\mathrm{2^n\:=\:2^{n-1}+2^{n-2}+.....+2^{0}+1}$

If the number is , the sum of all the powers of 2 starting from 0 to n-1 will give the number equals to (number-1).

We already know that any number represented in the binary form follows the same pattern starting from $\mathrm{2^{0}}$ to $\mathrm{2^{31}}$ in a 32-bit integer.

So if we want to subtract one from any number just find the first 1 bit and convert all the 0 bits after it to 1 and convert that 1 bit to 0, keeping the remaining bits same will give the number equals to N-1.

Let's check out the implementation of this algorithm in our approach.

Approach

  • We will create a variable ‘a’ to find the first 1 bit of the number.

  • For finding the first 1 bit of the number keep iterating in a while loop until (N&a)=0. (N&a) will return 1 only when both the bits are 1.

  • While iterating in the loop, keep updating the bits of the number N using the XOR operator until we find the first 1 bit of the number.

  • Along with updating the bits of the number N, left shift a every time to check for the first 1 bit.

  • Once we will find the first 1 bit the loop will break. We already updated all the bits after the first 1 bit with 1 until now.

  • Now using The XOR operator, we will update that first 1 bit of the number with 0 as XOR returns 0 when inputs are the same.

  • Then return the number N which will now be equal to N-1.

Below is the C++ code for the implementation of this approach:

Example

#include <bits/stdc++.h>

using namespace std;

//function to subtract 1 using bitwise operators
int subtract(int N){
   int a=1; //to find the first 1 bit of the number N
   
   while((N&a)==0){  //iterate in the loop until we find the first 1 bit as 0&1=0 and 1&1=1
      
    N=N^a; //keep updating the bits with 1 which are after first 1 bit of the number N
      
      a=a<<1; //left shift a by 1
   }
   N=N^a; //update the first 1 bit of the number with 0 as 1^1=0
   
   return N; //return the number which is equal to N-1
}

int main()
{
   int N=175;
   
   cout<<subtract(N)<<endl;
   
   N=56;
   cout<<subtract(N)<<endl;

   return 0;
}

Output

174
55

Time Complexity : O(log x) , x is equal to the number of bits in the given number N.

Space Complexity : O(1) , since we don't use any extra space.

Conclusion

The problem of subtracting 1 from the given number N without using any arithmetic operators, i.e. +,-,/,%,* in C++, is solved in this article with an effective method utilising the methodology of bit manipulation. In order to deduct 1 from the provided integer, bitwise operators were used.

I hope that this post helped you to understand every aspect of the problem.

Updated on: 21-Aug-2023

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements