编写一个程序,把一个数加到一个给定的数上。不允许使用诸如“+”、“-”、“*”、“/”、“++”、“–”等运算符。 例如:
null
Input: 12Output: 13Input: 6Output: 7
这个问题可以通过使用一些魔法来解决。以下是使用位运算符实现相同目的的不同方法。 方法1 要给数字x加1(比如0011000111),将最右边的0位(我们得到001100)后的所有位翻转 0 000). 最后,也翻转最右边的0位(我们得到0011001000)得到答案。
C++
// C++ code to add add // one to a given number #include <bits/stdc++.h> using namespace std; int addOne( int x) { int m = 1; // Flip all the set bits // until we find a 0 while ( x & m ) { x = x ^ m; m <<= 1; } // flip the rightmost 0 bit x = x ^ m; return x; } /* Driver program to test above functions*/ int main() { cout<<addOne(13); return 0; } // This code is contributed by rathbhupendra |
C
// C++ code to add add // one to a given number #include <stdio.h> int addOne( int x) { int m = 1; // Flip all the set bits // until we find a 0 while ( x & m ) { x = x ^ m; m <<= 1; } // flip the rightmost 0 bit x = x ^ m; return x; } /* Driver program to test above functions*/ int main() { printf ( "%d" , addOne(13)); getchar (); return 0; } |
JAVA
// Java code to add add // one to a given number class GFG { static int addOne( int x) { int m = 1 ; // Flip all the set bits // until we find a 0 while ( ( int )(x & m) >= 1 ) { x = x ^ m; m <<= 1 ; } // flip the rightmost 0 bit x = x ^ m; return x; } /* Driver program to test above functions*/ public static void main(String[] args) { System.out.println(addOne( 13 )); } } // This code is contributed by prerna saini. |
蟒蛇3
# Python3 code to add 1 # one to a given number def addOne(x) : m = 1 ; # Flip all the set bits # until we find a 0 while (x & m): x = x ^ m m << = 1 # flip the rightmost # 0 bit x = x ^ m return x # Driver program n = 13 print addOne(n) # This code is contributed by Prerna Saini. |
C#
// C# code to add one // to a given number using System; class GFG { static int addOne( int x) { int m = 1; // Flip all the set bits // until we find a 0 while ( ( int )(x & m) == 1) { x = x ^ m; m <<= 1; } // flip the rightmost 0 bit x = x ^ m; return x; } // Driver code public static void Main() { Console.WriteLine(addOne(13)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP code to add add // one to a given number function addOne( $x ) { $m = 1; // Flip all the set bits // until we find a 0 while ( $x & $m ) { $x = $x ^ $m ; $m <<= 1; } // flip the rightmost 0 bit $x = $x ^ $m ; return $x ; } // Driver Code echo addOne(13); // This code is contributed by vt_m. ?> |
Javascript
<script> // JavaScript code to add add // one to a given number function addOne( x) { let m = 1; // Flip all the set bits // until we find a 0 while ( x & m ) { x = x ^ m; m <<= 1; } // flip the rightmost 0 bit x = x ^ m; return x; } /* Driver program to test above functions*/ document.write(addOne(13)); </script> |
输出:
14
时间复杂性: O(原木) 2. n)
辅助空间: O(1)
方法2 我们知道,在大多数架构中,负数以2的补码形式表示。对于符号数的2的补码表示,我们有下面的引理。 比如说,x是一个数字的数值 ~x=-(x+1) [~表示按位补码] (x+1)是由于在2的补码转换中加了1 为了得到(x+1),再次应用否定。最后一个表达式变成(-(~x))。
C++
#include <bits/stdc++.h> using namespace std; int addOne( int x) { return (-(~x)); } /* Driver code*/ int main() { cout<<addOne(13); return 0; } // This code is contributed by rathbhupendra |
C
#include<stdio.h> int addOne( int x) { return (-(~x)); } /* Driver program to test above functions*/ int main() { printf ( "%d" , addOne(13)); getchar (); return 0; } |
JAVA
// Java code to Add 1 to a given number class GFG { static int addOne( int x) { return (-(~x)); } // Driver program public static void main(String[] args) { System.out.printf( "%d" , addOne( 13 )); } } // This code is contributed // by Smitha Dinesh Semwal |
蟒蛇3
# Python3 code to add 1 to a given number def addOne(x): return ( - (~x)); # Driver program print (addOne( 13 )) # This code is contributed by Smitha Dinesh Semwal |
C#
// C# code to Add 1 // to a given number using System; class GFG { static int addOne( int x) { return (-(~x)); } // Driver program public static void Main() { Console.WriteLine(addOne(13)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP Code to Add 1 // to a given number function addOne( $x ) { return (-(~ $x )); } // Driver Code echo addOne(13); // This code is contributed by vt_m. ?> |
Javascript
<script> // JavaScript program for the above approach function addOne(x) { return (-(~x)); } // Driver Code document.write(addOne(13)); // This code is contributed by susmitakundugoaldanga. </script> |
输出:
14
时间复杂性: O(1)
辅助空间: O(1)
例子:
Assume the machine word length is one *nibble* for simplicity.And x = 2 (0010),~x = ~2 = 1101 (13 numerical)-~x = -1101
以2的补码形式解释位1101产生的数值为-(2^4–13)=-3。在结果上应用“-”剩下3个。同样的类比适用于减量。请注意,只有当数字以2的补码形式存储时,此方法才有效。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END