C++编译期间字节序判断
当前常用的字节序一般就两种,大端序和小端序。
下面列出四种字节序的表达方式。在对应平台下,内存布局为{0x,00,0x01,0x02,0x03}的四字节,表示为十六进制的值就如下面代码所示的。
ENDIAN_BIG = 0x00010203, /* 大端序 ABCD */
ENDIAN_LITTLE = 0x03020100, /* 小端序 DCBA */
ENDIAN_BIG_WORD = 0x02030001, /* 中端序 CDAB, Honeywell 316 风格 */
ENDIAN_LITTLE_WORD = 0x01000302 /* 中端序 BADC, PDP-11 风格 */
gcc或clang中可以使用 __BYTE_ORDER__宏来判断
#include <stdio.h>
#include <stdlib.h>
int main()
{
// 这两个宏是gcc或者clang支持的
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
puts("小端序");
#elif __BYTE_ORDER__== __ORDER_BIG_ENDIAN__
puts("大端序");
#else
puts("未知字节序");
#endif // __BYTE_ORDER__
return 0;
}
还有使用C++ 11的constexpr关键字特性来做编译时判断的方法。但是我没有编译通过。
相关的可以看下面这两个网页
- 在编译时检查字节序
http://codereview.stackexchange.com/questions/45675/checking-endianness-at-compile-time - 编译器预定义宏
https://sourceforge.net/p/predef/wiki/Endianness/
C++编译期间字节序判断的更多相关文章
- 用C语言,如何判断主机是 大端还是小端(字节序)
所谓大端就是指高位值在内存中放低位地址,所谓小端是指低位值在内存中放低位地址.比如 0x12345678 在大端机上是 12345678,在小端机上是 78564312,而一个主机是大端还是小端要看C ...
- C/C++字节序(大端/小端)判断
C/C++大端小端判断 说的是变量的高字节.低字节在内存地址中的排放顺序. 变量的高字节放到内存的低地址中(变量的低字节放到内存的高地址中)==>大端 变量的高字节放到内存的高地址中(变量的低字 ...
- 判断.net中在windows系统下的字节序
字节序,是指字节在内存中的存储顺序.其又分为大端字节(Big-Endian)序和小端字节序(Little-Endian). 以下摘自百度百科: a) Little-Endian就是低位字节排放在内存的 ...
- 如何判断CPU字节序之[Big-endian vs Little-endian]
[本文链接] http://www.cnblogs.com/hellogiser/p/big-endian-vs-little-endian.html [Big-endian vs Little-en ...
- C语言字节对齐问题详解(对齐、字节序、网络序等)
首先说明一下,本文是转载自: http://www.cnblogs.com/clover-toeic/p/3853132.html 博客园用的少,不知道怎么发布转载文章,只能暂时这样了. 引言 考虑下 ...
- 字节序(byte order)和位序(bit order)
字节序(byte order)和位序(bit order) 在网络编程中经常会提到网络字节序和主机序,也就是说当一个对象由多个字节组成的时候需要注意对象的多个字节在内存中的顺序. 以前我也基本只了 ...
- linux kernel如何处理大端小端字节序
(转)http://blog.csdn.net/skyflying2012/article/details/43771179 最近在做将kernel由小端处理器(arm)向大端处理器(ppc)的移植的 ...
- 字节序:Big Endian 和 Little Endian
一.字节序 字节序,也就是字节的顺序,指的是多字节的数据在内存中的存放顺序. 在几乎所有的机器上,多字节对象都被存储为连续的字节序列.例如:如果C/C++中的一个int型变量 a 的起始地址是& ...
- 09.C语言:预处理(宏定义)、字节序、地址对齐
一.预处理 预处理 gcc -E Hello.c -o hello.i 编译 gcc -S hello.i -o hello.s 汇编 gcc -c hello.s -o hello.o 链接 gcc ...
随机推荐
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- Android自定义九宫格图案解锁
转自: http://blog.csdn.net/shineflowers/article/details/50408350
- python 补漏计划
从今天开始把python的细枝末节都梳理下 争取 每星期 两篇博文
- 纯css,编写菜单移入效果
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- javaMd5加密
package com.md5Test; import java.security.MessageDigest; public class Md5Test { public void toMD5(St ...
- svn报错cleanup failed–previous operation has not finished; run cleanup if it was interrupted的解决办法
今天在svn提交的时候它卡顿了一下,我以为已经提交完了,就按了一下,结果就再也恢复不了,也继续不了了... 报错 cleanup failed–previous operation has not f ...
- 【BZOJ-4197】寿司晚宴 状压DP
4197: [Noi2015]寿司晚宴 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 694 Solved: 440[Submit][Status] ...