Subtract 1 without arithmetic operators

24 Jun, 2022
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.
C++
C
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    cout << subtractOne(13) << endl;    return 0;} // This code is contributed by noob2000
// 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 code to subtract// one from a given numberimport 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 Codepublic static void main (String[] args){    System.out.println(subtractOne(13));}} // This code is contributed// by anuj_67.
# Python 3 code to subtract one from# a given numberdef 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 Codeif __name__ == '__main__':    print(subtractOne(13))     # This code is contributed by# Surendra_Gangwar
// C# code to subtract// one from a given numberusing 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 Codepublic static void Main (){    Console.WriteLine(subtractOne(13));}} // This code is contributed// by anuj_67.
<?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);     // This code is contributed// by anuj_67.?>
<script> // 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*/     document.write(subtractOne(13));  // This code is contributed by Surbhi Tyagi. </script>
12
 
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 – 1To obtain 2x, left shift x once. 
C++
C
Java
Python3
C#
PHP
Javascript
#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;} // This code is contributed by Aditya Kumar (adityakumar129)
#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;} // This code is contributed by Aditya Kumar (adityakumar129)
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));    }} // This code has been contributed by 29AjayKumar
def subtractOne(x):     return ((x << 1) + (~x)); # Driver codeprint(subtractOne(13)); # This code is contributed by mits
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));    }} // This code contributed by Rajput-Ji
<?phpfunction subtractOne($x){    return (($x << 1) + (~$x));} /* Driver code*/ print(subtractOne(13)); // This code has been contributed by mits?>
<script> function subtractOne(x){    return ((x << 1) + (~x));} /* Driver program to test above functions*/     document.write((subtractOne(13))); // This is code is contributed by Mayank Tyagi </script>
12
 
vt_m
SURENDRA_GANGWAR
29AjayKumar
Rajput-Ji
Mithun Kumar
surbhityagi15
mayanktyagi1709
noob2000
adityakumar129
programming-puzzle
Bit Magic
Mathematical
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.