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 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)); } } // This code is contributed // by anuj_67. |
Python3
# 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)) # This code is contributed by # Surendra_Gangwar |
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)); } } // This code is contributed // by anuj_67. |
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); // This code is contributed // by anuj_67. ?> |
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 – 1
To obtain 2x, left shift x once.
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
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 |
Python3
def subtractOne(x): return ((x << 1) + (~x)); # Driver code print(subtractOne(13)); # This code is contributed by mits |
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)); } } // This code contributed by Rajput-Ji |
PHP
<?php function subtractOne($x) { return (($x << 1) + (~$x)); } /* Driver code*/ print(subtractOne(13)); // This code has been contributed by mits ?> |
12
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Subtract two numbers without using arithmetic operators
- Add two numbers without using arithmetic operators
- How to sum two integers without using arithmetic operators in C/C++?
- Decimal to binary conversion without using arithmetic operators
- Smallest perfect power of 2 greater than n (without using arithmetic operators)
- Check if two numbers are equal without using arithmetic and comparison operators
- Check if n is divisible by power of 2 without using arithmetic operators
- Reverse an array without using subtract sign ‘-‘ anywhere in the code
- Operators in C | Set 2 (Relational and Logical Operators)
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Write you own Power without using multiplication(*) and division(/) operators
- Check if a number is multiple of 5 without using / and % operators
- Smallest of three integers without comparison operators
- Calculate 7n/8 without using division and multiplication operators
- C++ program to divide a number by 3 without using *, / , +, -, % operators
- Check if two numbers are equal without using comparison operators
- Largest in array without using conditionals / bitwise / ternary operators
- Find minimum in an array without using Relational Operators
- Find maximum in an array without using Relational Operators
- Largest of two distinct numbers without using any conditional statements or operators
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

