给定两个无符号整数(最大可能输入为32位)。任务是使用位运算将两个数字相加。
例子:
Input: n1 = 12, n2 = 34
Output: 46
Input: n1 = 12564 n2 = -1
Output: 12563
方法:由于我们知道位加法
- 1 + 0 = 1
- 0 + 1 = 1
- 0 + 0 = 0
- 1 + 1 = 0进位1
- if(进位== 1)1 + 1 = 1进位1
在C++中使用位集函数表示整数。它的行为就像一个数组,该数组在第0个索引处存储LSB(最低有效位),当我们打印这样的数组时,它会以相反的格式打印二进制表示形式。根据位加法属性从右开始添加每个位,并存储在第三个位集中。函数to_ulong()用于将位集形式转换为其十进制形式。
下面是上述方法的实现。
C++
#include
#define M 32
using namespace std;
// Function to add two bitset
int binAdd(bitset atemp, bitset btemp)
{
// To store the bits of answer
bitset ctemp;
for (int i = 0; i < M; i++)
ctemp[i] = 0;
// Initialize carry to 0
int carry = 0;
for (int i = 0; i < M; i++) {
// Both bits are zero
if (atemp[i] + btemp[i] == 0) {
if (carry == 0)
ctemp[i] = 0;
else {
ctemp[i] = 1;
carry = 0;
}
}
// Any of the one bit is 1
else if (atemp[i] + btemp[i] == 1) {
if (carry == 0)
ctemp[i] = 1;
else {
ctemp[i] = 0;
}
}
// Both bits are 1
else {
if (carry == 0) {
ctemp[i] = 0;
carry = 1;
}
else {
ctemp[i] = 1;
}
}
}
// To convert bitset into
// decimal equivalent
return ctemp.to_ulong();
}
// Driver Code
int main()
{
int number1, number2;
number1 = 12;
number2 = 34;
// Converting number 1 to bitset form
bitset num1(number1);
// Converting number 2 to bitset form
bitset num2(number2);
cout << binAdd(num1, num2) << endl;
}
Java
// Java program to add two
// unsigned numbers using bits
import java.util.*;
class GFG
{
static final int M = 32;
// Function to add two BitSet
static long binAdd(BitSet atemp,
BitSet btemp)
{
// To store the bits of answer
BitSet ctemp = new BitSet(M);
for (int i = 0; i < M; i++)
ctemp.set(i, false);
// Initialize carry to 0
int carry = 0;
for (int i = 0; i < M; i++)
{
// Both bits are zero
if (atemp.get(i) ==false &&
btemp.get(i) == false)
{
if (carry == 0)
ctemp.set(i, false);
else
{
ctemp.set(i, true);
carry = 0;
}
}
// Any of the one bit is 1
else if (atemp.get(i) == true||
btemp.get(i) == true)
{
if (carry == 0)
ctemp.set(i, true);
else
{
ctemp.set(i, false);
}
}
// Both bits are 1
else
{
if (carry == 0)
{
ctemp.set(i, false);
carry = 1;
}
else
{
ctemp.set(i, true);
}
}
}
// To convert BitSet into
// decimal equivalent
return ctemp.toLongArray()[0];
}
// Driver Code
public static void main(String args[])
{
int number1, number2;
number1 = 12;
number2 = 34;
// Converting number 1 to BitSet form
BitSet num1 = BitSet.valueOf(new long[]{number1});
// Converting number 2 to BitSet form
BitSet num2 = BitSet.valueOf(new long[]{number2});
System.out.println(binAdd(num1, num2) );
}
}
// This code is contributed
// by Arnab Kundu
Python3
# Python3 implementation of the approach
# Function to convert given Integer
# to list of bits of length M
def bitset(num):
return [int(x) for x in format(num, '032b')]
# Function to add two bitset
def binAdd(atemp, btemp):
# To store the bits of answer
ctemp = [0] * M
# Initialize carry to 0
carry = 0
for i in range(0, M):
# Both bits are zero
if atemp[i] + btemp[i] == 0:
if carry == 0:
ctemp[i] = 0
else:
ctemp[i] = 1
carry = 0
# Any of the one bit is 1
elif atemp[i] + btemp[i] == 1:
if carry == 0:
ctemp[i] = 1
else:
ctemp[i] = 0
# Both bits are 1
else:
if carry == 0:
ctemp[i] = 0
carry = 1
else:
ctemp[i] = 1
# To convert bitset into string and then
# convert string to its decimal equivalent
temp = ''.join([str(x) for x in ctemp])
return int(temp, 2)
# Driver Code
if __name__ == "__main__":
number1, number2 = 12, 34
M = 32
# Converting number 1 to bitset form
num1 = bitset(number1)
# Converting number 2 to bitset form
num2 = bitset(number2)
print(binAdd(num1, num2))
# This code is contributed by Rituraj Jain
输出:
46
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。