Subtract 1 without arithmetic operators
Last Updated :
07 Jan, 2024
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
Method 1
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++
#include <iostream>
using namespace std;
int subtractOne( int x)
{
int m = 1;
while (!(x & m))
{
x = x ^ m;
m <<= 1;
}
x = x ^ m;
return x;
}
int main()
{
cout << subtractOne(13) << endl;
return 0;
}
|
C
#include <stdio.h>
int subtractOne( int x)
{
int m = 1;
while (!(x & m)) {
x = x ^ m;
m <<= 1;
}
x = x ^ m;
return x;
}
int main()
{
printf ( "%d" , subtractOne(13));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int subtractOne( int x)
{
int m = 1 ;
while (!((x & m) > 0 ))
{
x = x ^ m;
m <<= 1 ;
}
x = x ^ m;
return x;
}
public static void main (String[] args)
{
System.out.println(subtractOne( 13 ));
}
}
|
Python3
def subtractOne(x):
m = 1
while ((x & m) = = False ):
x = x ^ m
m = m << 1
x = x ^ m
return x
if __name__ = = '__main__' :
print (subtractOne( 13 ))
|
C#
using System;
class GFG
{
static int subtractOne( int x)
{
int m = 1;
while (!((x & m) > 0))
{
x = x ^ m;
m <<= 1;
}
x = x ^ m;
return x;
}
public static void Main ()
{
Console.WriteLine(subtractOne(13));
}
}
|
PHP
<?php
function subtractOne( $x )
{
$m = 1;
while (!( $x & $m ))
{
$x = $x ^ $m ;
$m <<= 1;
}
$x = $x ^ $m ;
return $x ;
}
echo subtractOne(13);
?>
|
Javascript
<script>
function subtractOne(x)
{
let m = 1;
while (!(x & m)) {
x = x ^ m;
m <<= 1;
}
x = x ^ m;
return x;
}
document.write(subtractOne(13));
</script>
|
Time Complexity: O(log n), where n is the number of bits in x
Auxiliary Space: O(1)
Method 2 (If + is allowed)
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)); }
int main()
{
cout<< subtractOne(13);
return 0;
}
|
C
#include <stdio.h>
int subtractOne( int x) { return ((x << 1) + (~x)); }
int main()
{
printf ( "%d" , subtractOne(13));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int subtractOne( int x)
{
return ((x << 1 ) + (~x));
}
public static void main(String[] args)
{
System.out.printf( "%d" , subtractOne( 13 ));
}
}
|
Python3
def subtractOne(x):
return ((x << 1 ) + (~x));
print (subtractOne( 13 ));
|
C#
using System;
class GFG
{
static int subtractOne( int x)
{
return ((x << 1) + (~x));
}
public static void Main(String[] args)
{
Console.Write( "{0}" , subtractOne(13));
}
}
|
PHP
<?php
function subtractOne( $x )
{
return (( $x << 1) + (~ $x ));
}
print (subtractOne(13));
?>
|
Javascript
<script>
function subtractOne(x)
{
return ((x << 1) + (~x));
}
document.write((subtractOne(13)));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...