博客
关于我
【leetcode】 两整数之和, 不用+-号,如何实现两数相加
阅读量:547 次
发布时间:2019-03-09

本文共 2760 字,大约阅读时间需要 9 分钟。

Adding Two Integers Without Using '+' or '-' Operators

Adding two integers without using the '+' or '-' operators can be achieved using bitwise operations. The approach leverages the properties of the XOR and AND operators to simulate the addition process. Here's a detailed explanation of the method and a Python implementation.


Conceptual Understanding

In binary addition, each bit is processed from the least significant bit (LSB) to the most significant bit (MSB). When adding two bits:

  • XOR Operation: This gives the sum of the bits without considering carry.
  • AND Operation: This identifies positions where a carry is generated, which is then left-shifted to the next higher bit.
  • By iteratively applying these operations, the process continues until there are no more carries to propagate.


    Algorithm and Code

    def get_sum(a, b):    while (a & b) != 0:        shift = a & b        carry = shift << 1        a = a ^ b        b = carry    return a ^ b

    Step-by-Step Explanation:

  • Loop Until No Carry Exists:

    • The loop continues as long as there is a carry (i.e., a & b is not zero).
  • Calculate Current Bit Sum (XOR):

    • a ^ b computes the sum of the current bits without carry.
  • Calculate Next Carry (AND then Shift Left):

    • shift = a & b identifies bits where both a and b have a 1 (indicating a carry to the next higher bit).
    • carry = shift << 1 propagates the carry to the next higher bit.
  • Update a and b for Next Iteration:

    • a = a ^ b holds the current bit sum.
    • b = carry prepares the carry for the next iteration.
  • Termination:

    • When a & b becomes zero, there are no more carries, and the loop exits.
  • Return Result:

    • The final result is a ^ b, which now includes all the carries.

  • Example Demos

    Example 1:

    • Input: a = 1, b = 2
    • Binary: a = 01, b = 10
    • Iteration 1: XOR = 11 (3), AND = 01 → Shift left → carry = 10
    • Next values: a = 11, b = 10
    • Iteration 2: XOR = 01, AND = 10 → Shift left → carry = 100
    • Next values: a = 01, b = 100
    • Loop terminates (a & b = 000).
    • Result: 3.

    Example 2:

    • Input: a = -2, b = 3
    • Binary (assuming infinite bits for -2): ...1110 (a), 0000...0011 (b)
    • Iterations simulate correct handling of carry and signs, resulting in sum 1.

    Considerations and Limitations

    • Negative Numbers: The algorithm works for negative numbers in Python due to its arbitrary-precision integers.
    • Time Complexity: The algorithm processes each bit individually, leading to O(log n) time complexity.
    • Overflow: For languages with fixed-size integers, overflow may cause issues, but Python handles this seamlessly.

    Conclusion

    This method elegantly demonstrates how bitwise operations can be used to perform addition without using the '+' operator. By using XOR to sum bits and AND followed by a left shift to handle carries, the algorithm efficiently computes the sum of two integers.

    转载地址:http://qxhiz.baihongyu.com/

    你可能感兴趣的文章
    OpenCV与AI深度学习 | 如何使用YOLOv9检测图片和视频中的目标
    查看>>
    OpenCV与AI深度学习 | 如何在 Docker 容器中使用 GPU
    查看>>
    OpenCV与AI深度学习 | 实战 | OpenCV中更稳更快的找圆方法--EdgeDrawing使用演示(详细步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战 | OpenCV传统方法实现密集圆形分割与计数(详细步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战 | OpenCV实现扫描文本矫正应用与实现详解(附源码)
    查看>>
    OpenCV与AI深度学习 | 实战 | 使用OpenCV和Streamlit搭建虚拟化妆应用程序(附源码)
    查看>>
    OpenCV与AI深度学习 | 实战 | 使用OpenCV确定对象的方向(附源码)
    查看>>
    OpenCV与AI深度学习 | 实战 | 使用YOLOv8 Pose实现瑜伽姿势识别
    查看>>
    OpenCV与AI深度学习 | 实战 | 使用YoloV8实例分割识别猪的姿态(含数据集)
    查看>>
    OpenCV与AI深度学习 | 实战 | 使用姿态估计算法构建简单的健身训练辅助应用程序
    查看>>
    OpenCV与AI深度学习 | 实战 | 基于OpenCV和K-Means聚类实现颜色分割(步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战 | 基于YoloV5和Mask RCNN实现汽车表面划痕检测(步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战 | 基于YOLOv9+SAM实现动态目标检测和分割(步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战 | 基于YOLOv9和OpenCV实现车辆跟踪计数(步骤 + 源码)
    查看>>
    OpenCV与AI深度学习 | 实战 | 文本图片去水印--同时保持文本原始色彩(附源码)
    查看>>
    OpenCV与AI深度学习 | 实战—使用YOLOv8图像分割实现路面坑洞检测(步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战篇——基于YOLOv8和OpenCV实现车速检测(详细步骤 + 代码)
    查看>>
    OpenCV与AI深度学习 | 实战|OpenCV实时弯道检测(详细步骤+源码)
    查看>>
    OpenCV与AI深度学习 | 实践教程|旋转目标检测模型-TensorRT 部署(C++)
    查看>>
    OpenCV与AI深度学习 | 工业缺陷检测中数据标注需要注意的几个事项
    查看>>