Subtract 1 without arithmetic operators
Last Updated :
15 Feb, 2025
Write a program to subtract one from a given number. The use of operators like ‘+’, ‘-‘, ‘*’, ‘/’, ‘++’, ‘–‘ …etc are not allowed.
Examples:
Input: 12
Output: 11
Input: 6
Output: 5
Bitwise Subtraction Approach
To subtract 1 from a number x (say 0011001000), flip all the bits after the rightmost 1 bit (we get 0011001111). Finally, flip the rightmost 1 bit also (we get 0011000111) to get the answer.
Steps to solve this problem:
1. declare a variable m=1.
2. while x AND m is not equal to 1:
*update x as x XOR m.
*m<<=1.
3. update x as x XOR m.
4. return x.
C++
// C++ code to subtract
// one from a given number
#include <iostream>
using namespace std;
int subtractOne(int x)
{
int m = 1;
// Flip all the set bits
// until we find a 1
while (!(x & m))
{
x = x ^ m;
m <<= 1;
}
// Flip the rightmost 1 bit
x = x ^ m;
return x;
}
// Driver code
int main()
{
cout << subtractOne(13) << endl;
return 0;
}
C
// C code to subtract
// one from a given number
#include <stdio.h>
int subtractOne(int x)
{
int m = 1;
// Flip all the set bits
// until we find a 1
while (!(x & m)) {
x = x ^ m;
m <<= 1;
}
// flip the rightmost 1 bit
x = x ^ m;
return x;
}
/* Driver program to test above functions*/
int main()
{
printf("%d", subtractOne(13));
return 0;
}
Java
// Java code to subtract
// one from a given number
import java.io.*;
class GFG
{
static int subtractOne(int x)
{
int m = 1;
// Flip all the set bits
// until we find a 1
while (!((x & m) > 0))
{
x = x ^ m;
m <<= 1;
}
// flip the rightmost
// 1 bit
x = x ^ m;
return x;
}
// Driver Code
public static void main (String[] args)
{
System.out.println(subtractOne(13));
}
}
Python
# Python 3 code to subtract one from
# a given number
def subtractOne(x):
m = 1
# Flip all the set bits
# until we find a 1
while ((x & m) == False):
x = x ^ m
m = m << 1
# flip the rightmost 1 bit
x = x ^ m
return x
# Driver Code
if __name__ == '__main__':
print(subtractOne(13))
C#
// C# code to subtract
// one from a given number
using System;
class GFG
{
static int subtractOne(int x)
{
int m = 1;
// Flip all the set bits
// until we find a 1
while (!((x & m) > 0))
{
x = x ^ m;
m <<= 1;
}
// flip the rightmost
// 1 bit
x = x ^ m;
return x;
}
// Driver Code
public static void Main ()
{
Console.WriteLine(subtractOne(13));
}
}
JavaScript
// JavaScript code to subtract
// one from a given number
function subtractOne(x)
{
let m = 1;
// Flip all the set bits
// until we find a 1
while (!(x & m)) {
x = x ^ m;
m <<= 1;
}
// flip the rightmost 1 bit
x = x ^ m;
return x;
}
/* Driver program to test above functions*/
console.log (subtractOne(13));
PHP
<?php
// PHP code to subtract
// one from a given number
function subtractOne($x)
{
$m = 1;
// Flip all the set bits
// until we find a 1
while (!($x & $m))
{
$x = $x ^ $m;
$m <<= 1;
}
// flip the
// rightmost 1 bit
$x = $x ^ $m;
return $x;
}
// Driver Code
echo subtractOne(13);
?>
Time Complexity: O(log n), where n is the number of bits in x
Auxiliary Space: O(1)
Bitwise Negation and Shift Approach
We know that the negative number is represented in 2’s complement form on most of the architectures. We have the following lemma hold for 2’s complement representation of signed numbers.
Say, x is numerical value of a number, then
~x = -(x+1) [ ~ is for bitwise complement ]
Adding 2x on both the sides,
2x + ~x = x - 1
To obtain 2x, left shift x once.
C++
#include <bits/stdc++.h>
using namespace std;
int subtractOne(int x) { return ((x << 1) + (~x)); }
/* Driver program to test above functions*/
int main()
{
cout<< subtractOne(13);
return 0;
}
C
#include <stdio.h>
int subtractOne(int x) { return ((x << 1) + (~x)); }
/* Driver program to test above functions*/
int main()
{
printf("%d", subtractOne(13));
return 0;
}
Java
import java.io.*;
class GFG
{
static int subtractOne(int x)
{
return ((x << 1) + (~x));
}
/* Driver code*/
public static void main(String[] args)
{
System.out.printf("%d", subtractOne(13));
}
}
Python
def subtractOne(x):
return ((x << 1) + (~x));
# Driver code
print(subtractOne(13));
C#
using System;
class GFG
{
static int subtractOne(int x)
{
return ((x << 1) + (~x));
}
/* Driver code*/
public static void Main(String[] args)
{
Console.Write("{0}", subtractOne(13));
}
}
JavaScript
function subtractOne(x)
{
return ((x << 1) + (~x));
}
/* Driver program to test above functions*/
console.log((subtractOne(13)));
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Subtract two numbers without using arithmetic operators Write a function subtract(x, y) that returns x-y where x and y are integers. The function should not use any of the arithmetic operators (+, ++, â, -, .. etc). The idea is to use bitwise operators. Addition of two numbers has been discussed using Bitwise operators. Like addition, the idea is to use
8 min read
Add two numbers without using arithmetic operators Given two integers a and b, the task is to find the sum of a and b without using + or - operators. Examples: Input: a = 10, b = 30Output: 40Input: a = -1, b = 2Output: 1Approach:The approach is to add two numbers using bitwise operations. Let's first go through some observations: a & b will have
5 min read
Division without using '/' operator Given two numbers, divide one from other without using '/' operator. Examples : Input : num1 = 13, num2 = 2 Output : 6 Input : num1 = 14, num2 = -2 Output : -7 Input : num1 = -11, num2 = 3 Output : -3 Input : num1 = 10, num2 = 10 Output : 1 Input : num1 = -6, num2 = -2 Output : 3 Input : num1 = 0, n
6 min read
Arithmetic Expression Evaluation The stack organization is very effective in evaluating arithmetic expressions. Expressions are usually represented in what is known as Infix notation, in which each operator is written between two operands (i.e., A + B). With this notation, we must distinguish between ( A + B )*C and A + ( B * C ) b
2 min read
Implement *, - and / operations using only + arithmetic operator Given two numbers, perform multiplication, subtraction, and division operations on them, using '+' arithmetic operator only. Operations can be performed as follows: Subtraction :- a - b = a + (-1)*b. Multiplication :- a * b = a + a + a ... b times. Division :- a / b = continuously subtract b from a
12 min read
Arithmetic Circuits Arithmetic circuits are fundamental blocks in digital systems and are used for arithmetic operations such as addition, subtraction, multiplication and division. These circuits constitute the major stream of computation in different applications such as processors, digital signal processing units, an
7 min read
Perl | Operators | Set - 1 Operators are the main building block of any programming language. Operators allow the programmer to perform different kinds of operations on operands. In Perl, operators symbols will be different for different kind of operands(like scalars and string). Operators Can be categorized based upon their
12 min read
Find sequence of operations to convert 1 to N using Multiply-by-2-Add-1 or Subtract-1 Given an integer N, the task is to find a sequence of operations to convert 1 to N. You can perform any number of operations, and the goal is to reach the number N using these operations. If it's not possible to reach N, return -1. There are two possible operations that can be performed on the curre
6 min read
Multiplying a variable with a constant without using multiplication operator As we know that every number can be represented as sum(or difference) of powers of 2, therefore what we can do is represent the constant as a sum of powers of 2.For this purpose we can use the bitwise left shift operator. When a number is bitwise left shifted it is multiplied by 2 for every bit shif
4 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 = 2Input: a = 20, b = 0Output: a = 0, b = 20Input: a = 10, b = 10Output: a = 10, b = 10Table of ContentUsing Arithmetic OperatorsUsing Bitwise XORBuilt-in SwapUsing Arithmeti
6 min read