30/09/2018, 21:59

Giải thích giúp em tại sao ra như vậy?

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

int main(int argc, char *argv[]) {
	
	typedef union
	{
		int ival;
		float fval;
		char ch[2];
	}value;
	
	 value a, b, x[10];
	 a.ival=0xa1b2;
	 printf("cha[0]=%x, char[1]=%x
", a.ch[0], a.ch[1]);
	 
	return 0;
}

Tại sao a.ch[0]=0xb2 , a.ch[1]=0xa1 ??? Mà không phải là a.ch[0]=0xa1 , a.ch[1]=0xb2 ???

Itachi Citus viết 00:14 ngày 01/10/2018

Nó do quy định cách lưu trữ của máy tính thôi bạn. Nếu lưu theo little-endian thì byte thấp lưu ở trước, byte cao lưu ở sau. Big-endian thì ngược lại. Các dòng máy tính sử dụng kiến trúc x86-64 lưu trữ theo little-endian nên thu được kết quả trên.

Big-endian and little-endian are terms that describe the order in which a sequence of bytes are stored in computer memory. Big-endian is an order in which the “big end” (most significant value in the sequence) is stored first (at the lowest storage address). Little-endian is an order in which the “little end” (least significant value in the sequence) is stored first. For example, in a big-endian computer, the two bytes required for the hexadecimal number 4F52 would be stored as 4F52 in storage (if 4F is stored at storage address 1000, for example, 52 will be at address 1001). In a little-endian system, it would be stored as 524F (52 at address 1000, 4F at 1001).
http://searchnetworking.techtarget.com/definition/big-endian-and-little-endian

Bài liên quan
0