编写一个程序,将一个数字加到给定的数字上。不允许使用“ +”,“-”,“ *”,“ /”,“ ++”,“ –”等运算符。
例子:
Input: 12
Output: 13
Input: 6
Output: 7
这个问题可以通过使用一些魔术来解决。以下是使用按位运算运算符实现相同效果的不同方法。
方法1
要将1加到数字x(例如0011000111),请翻转最右边0位之后的所有位(我们得到001100 0 000)。最后,也将最右边的0位翻转(我们得到0011001000)以获得答案。
C++
// C++ code to add add
// one to a given number
#include
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<
C
// C++ code to add add
// one to a given number
#include
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.
Python3
# 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
C++
#include
using namespace std;
int addOne(int x)
{
return (-(~x));
}
/* Driver code*/
int main()
{
cout<
C
#include
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
Python3
# 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
Javascript
输出:
14
方法二
我们知道,在大多数体系结构中,负数均以2的补码形式表示。对于符号数的2的补码表示,我们具有以下引理成立。
假设x是数字的数值,则
〜x =-(x + 1)[〜用于按位补码]
(x +1)是由于2的补码转换加了1
要获得(x + 1),请再次应用负号。因此,最终表达式变为(-(〜x))。
C++
#include
using namespace std;
int addOne(int x)
{
return (-(~x));
}
/* Driver code*/
int main()
{
cout<
C
#include
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
Python3
# 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
Java脚本
输出:
14
例子 :
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的补码形式存储时,此方法才有效。
https://www.youtube.com/watch?v=wP5P2
-5n8iU