Escriba un programa para restar uno de un número dado. No se permite el uso de operadores como ‘+’, ‘-‘, ‘*’, ‘/’, ‘++’, ‘–’…etc.
Ejemplos:
Input: 12 Output: 11 Input: 6 Output: 5
Método 1
Para restar 1 de un número x (por ejemplo, 0011001000), voltea todos los bits después del 1 bit más a la derecha (obtenemos 001100 1 111). Finalmente, cambie también el bit más a la derecha (obtenemos 0011000111) para obtener la respuesta.
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; } // This code is contributed by noob2000
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. ?>
Javascript
<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
Método 2 (si se permite +)
Sabemos que el número negativo se representa en forma de complemento a 2 en la mayoría de las arquitecturas. Tenemos el siguiente lema válido para la representación en complemento a 2 de números con signo.
Digamos que x es el valor numérico de un número, entonces
~x = -(x+1) [ ~ es para complemento bit a bit ]
Sumar 2x en ambos lados,
2x + ~x = x – 1
Para obtener 2x, desplaza x a la izquierda una vez .
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; } // This code is contributed by Aditya Kumar (adityakumar129)
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; } // This code is contributed by Aditya Kumar (adityakumar129)
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 ?>
Javascript
<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
Publicación traducida automáticamente
Artículo escrito por aganjali10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA