법, 용어/용어

Big-Endian 큰수끝 Little-Endian 작은수끝

3604 2025. 5. 1. 17:17
728x90

ㅁ 메모리 구조

메모리의 끝이 큰수임

Address:   00   01   02   03
Data:         12   34   56   78

RISC CPU 계열

메모리의 끝이 작은수임

Address:   00   01   02   03
Data:        78   56   34   12

Intel CPU

출처: https://velog.io/@koey_h/bigendianvslittleendian

비트(Bit)와 바이트(Byte)

먼저 기본적인 단위부터 다시 짚고 넘어가보자.

비트(bit) : 데이터의 최소 단위로 2진수의 값 (0 또는 1)을 단 하나만 저장할 수 있다.
바이트(byte) : 1byte = 8bit, 일반적인 ANSI text 기준 한 문자는 1byte로 표현된다. (한글과 한자는 2byte)


바이트 저장 순서(Byte order)

메모리 1칸은 1byte로 구성되어 있지만 컴퓨터가 저장하는 데이터는 대개 4byte나 8byte로 구성된다. 따라서 컴퓨터가 데이터를 메모리에 저장할 때 연속되는 바이트를 나눠서 저장해야 하는데, 이 저장하는 순서를 바이트 저장 순서라고 한다. 그리고 이 순서에 따라 오늘의 주제인 빅 엔디안과 리틀 엔디안 방식으로 나뉘는 것이다.


빅 엔디안(Big Endian)

낮은 주소에 데이터의 높은 바이트(MSB, Most Significant Bit) 부터 저장하는 방식

위의 두 그림은 이해를 돕기위한 메모리의 대략적인 생김새와 MSB, LSB에 대한 그림이다.

즉, 예를 들어 4byte 크기의 정수 0x12345678을 빅 엔디안 방식으로 저장한다면

이런식으로 저장되는 것이다.

이 방식은 평소 우리가 숫자를 사용하는 방식과 같은 방식이므로 메모리에 저장된 순서 그대로 읽을 수 있으며, 이해하기 쉽다는 장점을 가지고 있어 데이터의 각 바이트를 배열처럼 취급할 때에는 빅 엔디안 방식이 적합하다. SPARC을 포함한 대부분의 RISC CPU 계열에서는 이 방식으로 데이터를 저장하며 네트워크를 통해 데이터를 전송할 때에는 빅 엔디안 방식이 사용된다.


리틀 엔디안(Little Endian)

낮은 주소에 데이터의 낮은 바이트(LSB, Least Significant Bit)부터 저장하는 방식

즉, 예를 들어 4byte 크기의 정수 0x12345678을 빅 엔디안 방식으로 저장한다면

이런식으로 저장되는 것이다.

이 방식은 평소 우리가 숫자를 사용하는 방식과는 반대로 거꾸로 읽어야 하지만 물리적으로 데이터를 조작하거나 산술 연산을 수행할 때에는 더 효율적이다. 대부분의 Intel CPU 계열에서는 이 방식으로 데이터를 저장한다.


내 컴퓨터는 빅 엔디안? 리틀 엔디안?

그렇다면 내가 사용하는 Macbook의 M1칩은 어떤 방식을 사용할까? Xcode를 사용하여 확인해 보았다.

다음의 간단한 코드를 작성하여 디버깅 모드로 진입 후 a의 메모리를 확인해 본 결과

#include <iostream>
using std::cout;
using std::endl;

int main() {
    int a = 98;
    cout << "int형 변수 a의 주소값 : " << &a << "\nint형 변수 a의 값 : " << a << endl;
    
    return 0;
}

98을 4byte 형식의 16진수로 나타내면 0x00000062 인데 메모리의 낮은 주소 부터 0x62 -> 0x00 -> 0x00 -> 0x00이 차례로 저장되어 있는 것을 볼 수 있다. 따라서 내가 사용하는 m1은 리틀 엔디안 방식으로 작동한다는 것을 알 수 있다. 😁


참고자료

53) 바이트 저장 순서 - TCP SCHOOL.com
LSB와 MSB란 무엇인가? - 땜쓰의 전자연구소
메모리구조 [Memory Structure] - Stranger's LAB

 

 

출처: https://www.geeksforgeeks.org/little-and-big-endian-mystery/

What is Endianness? Big-Endian & Little-Endian

Last Updated : 23 May, 2024
 
 
 

Computers operate using binary code, a language made up of 0s and 1s. This binary code forms the foundation of all computer operations, enabling everything from rendering videos to processing complex algorithms. A single bit is a 0 or a 1, and eight bits make up a byte. While some data, such as certain English characters, can be represented by a single byte, other data types require multiple bytes. The concept of endianness is crucial in understanding how these bytes are read and interpreted by computers.

What is Endianness?

Endianness refers to the order in which bytes are arranged in memory. Different languages read their text in different orders. for example, English reads from left to right, while Arabic reads from right to left. Endianness works similarly for computers. If one computer reads bytes from left to right and another reads them from right to left, issues arise when these computers need to communicate.

Endianness ensures that bytes in computer memory are read in a specific order. Each computer system is internally consistent with its own data, but the advent of the internet has led to more data sharing than ever before, and not all systems read data in the same order.

Endianness comes in two primary forms: Big-endian (BE) and Little-endian (LE).

  • Big-endian (BE): Stores the most significant byte (the “big end”) first. This means that the first byte (at the lowest memory address) is the largest, which makes the most sense to people who read left to right.
  • Little-endian (LE): Stores the least significant byte (the “little end”) first. This means that the first byte (at the lowest memory address) is the smallest, which makes the most sense to people who read right to left.

What is Big-endian?

In a big-endian system, the most significant byte (MSB) is stored at the lowest memory address. This means the “big end” (the most significant part of the data) comes first. For instance, a 32-bit integer 0x12345678 would be stored in memory as follows in a big-endian system:

 
Address:   00   01   02   03
Data:         12   34   56   78
 

Here, 0x12 is the most significant byte, placed at the lowest address (00), followed by 0x34, 0x56, and 0x78 at the highest address (03).

What is Little-endian?

A little-endian system stores the least significant byte (LSB) at the lowest memory address. The “little end” (the least significant part of the data) comes first. For the same 32-bit integer 0x12345678, a little-endian system would store it as:

 
Address:   00   01   02   03
Data:        78   56   34   12
 

Here, 0x78 is the least significant byte, placed at the lowest address (00), followed by 0x56, 0x34, and 0x12 at the highest address (03).

Significance of Most Significant Byte (MSbyte) in Little and Big Endian:

Understanding the concept of the Most Significant Byte (MSbyte) helps clarify endianness further. Let’s use a decimal number to illustrate.

Consider the decimal number 2,984. Changing the digit 4 to 5 increases the number by 1, while changing the digit 2 to 3 increases the number by 1,000. This concept applies to bytes and bits as well.

  • Most Significant Byte (MSbyte): The byte that holds the highest position value.
  • Least Significant Byte (LSbyte): The byte that holds the lowest position value.

In big-endian format, the MSbyte is stored first. In little-endian format, the MSbyte is stored last.

When Might Endianness Be an Issue?

Endianness must be considered in various computing scenarios, particularly when systems with different byte orders need to communicate or share data.

  1. Unicode Characters: Unicode, the character set used universally across devices, uses a special character byte sequence called the Byte Order Mark (BOM). The BOM informs the system that the incoming stream is Unicode, specifies which Unicode character encoding is used, and indicates the endian order of the incoming stream.
  2. Programming Languages: Some programming languages require specifying the byte order sequence. For instance, in Swift, used for iOS development, you can define whether data is stored in big-endian or little-endian format.
  3. Network Protocols: Different protocols have emerged historically, leading to the need for interaction. Big-endian is the dominant order in network protocols and is referred to as network order. Conversely, most PCs use little-endian format. Ensuring interoperability between these formats is critical in network communication.
  4. Processor Design: Processors can be designed to be either little-endian, big-endian, or bi-endian (capable of handling both). Consumer choice and the resulting market trends have influenced what is considered “normal” in computer systems today.

Why is Endianness an Issue?

Endianness becomes an issue primarily due to the interaction between different systems and protocols. Historical protocol development led to varying byte order conventions, necessitating data conversion for compatibility. In higher-level languages and abstracted environments, endianness is often managed behind the scenes, reducing the need for developer concern. However, understanding endianness remains crucial for low-level programming, network protocol design, and data interoperability.

Conclusion

Endianness is how bytes are ordered in computer data. Big-endian and little-endian are two ways to arrange bytes, each with advantages. Understanding endianness is very important for developers dealing with low-level data, networking, and system interoperability. While little-endian is common, both formats remain important as technology evolves. Strategies for managing data across endian conventions will continue developing to ensure compatibility and performance.

728x90