1. 程式人生 > >編寫一個程式,判斷系統是big endian,還是little endian

編寫一個程式,判斷系統是big endian,還是little endian

// Endian_1.cpp : 定義控制檯應用程式的入口點。
//

#include "stdafx.h"
#include <stdlib.h>  
#include <stdio.h>  
#include <iostream>  
using namespace std; 

int _tmain(int argc, _TCHAR* argv[])
{

	long ii = 0x123456;
	long *pp=ⅈ

	if(*pp==0x56){////低位元組存放在低地址端,即是首地址
	 cout<<"system is little endian"<<endl;
	 
	}
	else{
	 cout<<"system is big endian"<<endl;
	}
	
	
	 long i = 0x123456;
     long *p = &i;

    if (*p==0x56)
    {
        printf("This is little endian system\n");
    }
    else
    {
        printf("This is big endian system\n");
    } 

	system("pause");
	return 0;
}

//system is big endian
//This is big endian system
//請按任意鍵繼續. . .

JAVA位元組序

BIG-ENDIANLITTLE-ENDIAN跟多位元組型別的資料有關,比如int,short,long型,而對單位元組資料byte卻沒有影響。BIG-ENDIAN就是低位位元組排放在記憶體的高階,高位位元組排放在記憶體的低端。而LITTLE-ENDIAN正好相反。

比如 int a = 0x05060708

BIG-ENDIAN的情況下存放為:

位元組號 0 1 2 3     高地址端(高階)

高位元組  資料 05 06 07 08

LITTLE-ENDIAN的情況下存放為:

位元組號 0 1 2 3 高地址端(高階)

資料 08 07 06 05高位元組