gpt4 book ai didi

c - 使用 C 确定系统是使用 Big Endian 还是 Little Endian

转载 作者:行者123 更新时间:2023-12-04 10:16:13 26 4
gpt4 key购买 nike

我很好奇这个函数是否会决定字节顺序。

测试是一个位掩码,如果整数 someInt 存储在小端中,则该位掩码等于 1。

在位掩码中,0x1000 会被转换以匹配机器的字节序样式还是“常量”?

#include <stdio.h>

int isBigEndian(){
int someInt =0x0001;
if(someInt & 0x0100 == 1)
return 1;
else
return 0;
}

int main(){
int returnVal = isBigEndian();
printf("return val is %d", returnVal);
}

最佳答案

该函数将始终返回零,因为您的常量也以系统的 native 字节顺序存储。一个更好的选择是只使用一个系统 API 来回答你的问题。如果你必须是可移植的,你可能可以比较 val == ntohl(val) 来决定字节顺序。

关于c - 使用 C 确定系统是使用 Big Endian 还是 Little Endian,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6719339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com