此代码将如何在大型endian机器上工作? [英] How will this code will work on a big endian machine?

查看:64
本文介绍了此代码将如何在大型endian机器上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有代码:

uint64_t a = 0x1111222233334444;
uint32_t b = 0;
b = a;
printf("a is %llx ",a);
printf("b is %x ",b);

,输出为:

 a is 1111222233334444 b is 33334444

问题 >:


  1. 在大端存储机器上的行为是否相同?

  1. Will the behavior be same on big-endian machine?

如果我在b中分配a的值或进行类型转换,则结果在big endian中是否相同?

If I assign a's value in b or do a typecast will the result be same in big endian?


推荐答案

您那里的代码将以相同的方式工作。这是因为向下转换的行为是由C标准定义的。

The code you have there will work the same way. This is because the behavior of downcasting is defined by the C standard.

但是,如果您这样做:

uint64_t a = 0x0123456789abcdefull;
uint32_t b = *(uint32_t*)&a;
printf("b is %x",b)

然后它将是endian-

Then it will be endian-dependent.

编辑:

小尾数法:b为89abcdef

Little Endian: b is 89abcdef

Big Endian:b是01234567

Big Endian : b is 01234567

这篇关于此代码将如何在大型endian机器上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆