c - 二元加法

标签 c bit-shift

我想了解按位运算符在 C 中的工作原理, 但我对 << 有误解运营商。

我有以下代码:

#include <stdio.h>

int Add(int x, int y);
int Add(int x, int y)
{
    while ( y != 0 )        /// Iterate till there is no carry
    {
        int carry = x & y;  /// carry now contains common set bits of x and y
        x = x ^ y;          /// Sum of bits of x and y where at least one of the bits is not set
        y = carry << 1;     /// Carry is shifted by one so that adding it to x gives the required sum
    }
    return x;
}

int main( void )
{
    printf( "%d", Add( 13, 17 ) );
    return 0;
}

如果我理解正确的话是这样的:

First Iteration:
|=======================================|
|                                       |
|   while ( y       !=      0     )     |
|   while ( 17      !=      0     )     |
|   while ( 10001   !=      00000 )     |
|                                       |
|   c       =   x       &   y;          |
|   1       =   13      &   17          |
|   00001   =   01101   &   10001       |
|                                       |
|   x       =   x       ^   y           |
|   28      =   13      ^   17          |
|   11100   =   01101   ^   10001       |
|                                       |
|   y       =   c      <<   1           |
|   17      =   1      <<   1           |
|   10001   =   00001  <<   00001       |
|   00010   =   00001  <<   00001       |
|                                       |
|=======================================|

Second Iteration:
|=======================================|
|                                       |
|   while ( y       !=      0     )     |
|   while ( 2       !=      0     )     |
|   while ( 00010   !=      00000 )     |
|                                       |
|   c       =   x       &   y;          |
|   0       =   28      &   2           |
|   00000   =   11100   &   00010       |
|                                       |
|   x       =   x       ^   y           |
|   30      =   28      ^   2           |
|   11110   =   11100   ^   00010       |
|                                       |
|   y       =   c      <<   1           |
|   2       =   0      <<   1           |
|   00010   =   00000  <<   00001       |
|   00000   =   00000  <<   00001       |
|                                       |
|=======================================|

然后 Y变成 0X返回 30 .

现在在下面的代码中我遇到了一个问题:

#include <stdio.h>

int main( void )
{
    int x = 13;
    int y = x << 1; /// 11010 = 01101 << 00001
    x = 0 << 1;     /// 00000 = 00000 << 00001

    printf("y = %d\n", y ); /// 26  | 11010
    printf("x = %d\n", x ); /// 26  | 11010
    return 0;
}

这里,如果我理解正确,我们将所有位向左移动一位:

int y = x << 1; /// 11010 = 01101 << 00001

但是这里到底发生了什么:

x = 0 << 1;     /// 00000 = 00000 << 00001

是否 x清除并充满 0 << 1 的结果?

最佳答案

Does x get cleared and filled with the rezult of 0 << 1 ?

x刚被赋予表达式 0 << 1 的值.左移或右移任意量的零仍然是 0 .

So this means that the representation of the First and Second Iteration is correct?

这是正确的,除了变量的旧值(在 lhs 上)的替换有点困惑,如以下情况。

17      =   1      <<   1 
10001   =   00001  <<   00001 

2       =   0      <<   1
00010   =   00000  <<   00001

而是将其描述为:

y      =   1      <<   1 
y      =   00001  <<   00001 

y       =   0      <<   1
y       =   00000  <<   00001

关于c - 二元加法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54073837/

相关文章:

c# - 为什么我在 Powershell 和 C# 之间得到不同的结果

c++ - 被未定义的 C++ 移位运算符行为和包装混淆 "pattern space"

c - 错误 : conflicting types for ‘wstat’ in gcc

c - 向数组添加零填充

c - 如何为指针分配内存

c++ - Macports cgal 不工作!我如何解决它?

Java位移操作: error converting int to byte

c - 整数<<32和<<31<<1之间的区别

python - 为什么 while(n) n = n>>1 循环不会以负 n 终止

C 程序按值传递