C中移位运算符的具体用法是什么

发布于 2024-12-09 13:16:32 字数 692 浏览 0 评论 0原文

我认为移位运算符会移位应用它的整数或字符的内存,但以下代码的输出让我感到惊讶。

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main(void) {
    uint64_t number = 33550336;

    unsigned char *p = (unsigned char *)&number;
    size_t i;
    for (i=0; i < sizeof number; ++i)
        printf("%02x ", p[i]);
    printf("\n");

    //shift operation
    number = number<<4;

    p = (unsigned char *)&number;
    for (i=0; i < sizeof number; ++i)
        printf("%02x ", p[i]);
    printf("\n");

    return 0;
}

它运行的系统是小端并产生以下输出:

00 f0 ff 01 00 00 00 00 
00 00 ff 1f 00 00 00 00 

有人可以提供一些关于轮班运算符的详细工作的参考吗?

I thought shift operator shifts the memory of the integer or the char on which it is applied but the output of the following code came a surprise to me.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main(void) {
    uint64_t number = 33550336;

    unsigned char *p = (unsigned char *)&number;
    size_t i;
    for (i=0; i < sizeof number; ++i)
        printf("%02x ", p[i]);
    printf("\n");

    //shift operation
    number = number<<4;

    p = (unsigned char *)&number;
    for (i=0; i < sizeof number; ++i)
        printf("%02x ", p[i]);
    printf("\n");

    return 0;
}

The system on which it ran is little endian and produced the following output:

00 f0 ff 01 00 00 00 00 
00 00 ff 1f 00 00 00 00 

Can somebody provide some reference to the detailed working of the shift operators?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故人爱我别走 2024-12-16 13:16:32

我想你已经回答了你自己的问题。机器是小端,这意味着字节存储在内存中,最低有效字节位于左侧。所以你的记忆代表:

00 f0 ff 01 00 00 00 00 => 0x0000000001fff000
00 00 ff 1f 00 00 00 00 => 0x000000001fff0000

如你所见,第二个值与第一个值相同,左移 4 位。

I think you've answered your own question. The machine is little endian, which means the bytes are stored in memory with the least significant byte to the left. So your memory represents:

00 f0 ff 01 00 00 00 00 => 0x0000000001fff000
00 00 ff 1f 00 00 00 00 => 0x000000001fff0000

As you can see, the second is the same as the first value, shifted left by 4 bits.

久伴你 2024-12-16 13:16:32

一切正确:

(1 * (256^3)) + (0xff * (256^2)) + (0xf0 * 256) = 33 550 336

(0x1f * (256^3)) + (0xff * (256^2) )) = 536 805 376

33 550 336 * (2^4) = 536 805 376

左移 4 位与乘以 2^4 相同。

Everything is right:

(1 * (256^3)) + (0xff * (256^2)) + (0xf0 * 256) = 33 550 336

(0x1f * (256^3)) + (0xff * (256^2)) = 536 805 376

33 550 336 * (2^4) = 536 805 376

Shifting left by 4 bits is the same as multiplying by 2^4.

半步萧音过轻尘 2024-12-16 13:16:32

我认为 printf 让你感到困惑。以下是这些值:

     33550336 = 0x01FFF000
33550336 << 4 = 0x1FFF0000

您现在能读取输出吗?

I think you printf confuses you. Here are the values:

     33550336 = 0x01FFF000
33550336 << 4 = 0x1FFF0000

Can you read you output now?

森罗 2024-12-16 13:16:32

它不会移动内存,而是移动位。所以你得到了这个数字:

00 00 00 00 01 FF F0 00

将这个数字向左移动 4 位(一个十六进制数字)后,你得到:

00 00 00 00 1F FF 00 00

这正是转换为小尾数法时得到的输出。

It doesn't shift the memory, but the bits. So you have the number:

00 00 00 00 01 FF F0 00

After shifting this number 4 bits (one hexadecimal digit) to the left you have:

00 00 00 00 1F FF 00 00

Which is exactly the output you get, when transformed to little endian.

一个人的旅程 2024-12-16 13:16:32

您的循环按照字节存储在内存中的顺序打印字节,并且在大端机器上输出会有所不同。如果您想以十六进制打印该值,只需使用 %016llx。然后您将看到您所期望的结果:

0000000001fff000
000000001fff0000

第二个值左移 4。

Your loop is printing bytes in the order they are stored in memory, and the output would be different on a big-endian machine. If you want to print the value in hex just use %016llx. Then you'll see what you expect:

0000000001fff000
000000001fff0000

The second value is left-shifted by 4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文