5.1.11 · D4 · HinglishInstruction Set Architecture (ISA)

ExercisesEndianness (big vs little)

1,954 words9 min read↑ Read in English

5.1.11 · D4 · Hardware › Instruction Set Architecture (ISA) › Endianness (big vs little)

Shuru karne se pehle, ek reminder us single formula ka jis par poora page tika hua hai. Ek -byte value ke liye, uske bytes ko se number karo jahan least significant byte (LSB) hai. Tab

  • low bytes ko raste se shift out karta hai (har byte 8 bits ka hota hai).
  • baaki sab kuch mask karke sirf sabse neeche wala byte bachata hai.

Aur base address , offset par memory mein placement:

  • Little-endian: (LSB jaata hai lowest address par)
  • Big-endian: (MSB jaata hai lowest address par)

Neeche diya reference figure har problem ke liye mental picture hai: addresses par chaar mailboxes, aur unhe bharne ke do rules.

Figure — Endianness (big vs little)

L1 — Recognition

Recall Solution 1.1

HUM KYA CHECK KARTE HAIN: kya lowest address wala byte value ke most significant byte se match karta hai ya least significant byte se? Value ka MSB hai 0xAA (sabse baaya pair, weight ). Lowest address par AA hai. MSB at lowest address ⇒ big-endian ("Big end → Beginning").

Recall Solution 1.2

Lowest address par DD hai, jo LSB hai (weight ). LSB at lowest address ⇒ little-endian.

Recall Solution 1.3

Big-endian. Dekho Network Protocols — isi liye htonl/ntohl exist karte hain: yeh host ke order aur is agreed big-endian wire order ke beech convert karte hain.


L2 — Application

Recall Solution 2.1

Bytes extract karo ( is LSB): . Kyun: ; e.g. . Place karo little-endian (): 200:EF 201:BE 202:AD 203:DE.

Recall Solution 2.2

Big-endian index ko reverse karta hai: . 200:DE 201:AD 202:BE 203:EF. Check: sabse bada byte (DE) sabse chhote address par pada — "Big end first" se match karta hai.

Recall Solution 2.3

Bytes: (LSB), (MSB). Little-endian (): 10:FF 11:00. Big-endian (): 10:00 11:FF. kyun matter karta hai: big-endian index hota hai, isliye reversal ki length value ki width par depend karti hai.


L3 — Analysis

Recall Solution 3.1

Little-endian mein offset wala byte hi byte index hai, toh ise se weight karo:

Recall Solution 3.2

Big-endian mein , toh offset wala byte MSB hai. Lowest address ko sabse zyada weight do: Lesson: identical bytes, opposite convention ⇒ ek bilkul alag number. Isi liye do machines ka agree karna zaroori hai.

Recall Solution 3.3

Writer (little-endian) rakhta hai: 0:44 1:33 2:22 3:11. Reader (big-endian) offset ko MSB maanta hai: Value byte-reversed ho gayi — classic bug jo Network Protocols mein ntohl se fix hota hai.


L4 — Synthesis

Recall Solution 4.1

Little-endian bytes: 0:09 1:00 2:00 3:00. Address 0 par ek byte = 0x09 = 9 — sahi low-order value, widening "free" hai. Big-endian bytes: 0:00 1:00 2:00 3:09. Address 0 par ek byte = 0x00 = 0 — galat; 9 address 3 par hai. Kyun: little-endian low-order data ko low addresses par rakhta hai, isliye narrow reads directly kaam karte hain (dekho Memory Addressing).

Recall Solution 4.2

ke bytes hain , . Little-endian: lowest address par hai ⇒ b == 1. Big-endian: lowest address par MSB hai ⇒ b == 0. Single nonzero byte ki location convention reveal karti hai.

Recall Solution 4.3

Equivalently . Yeh endianness se independent hai: shifts value (abstract number) par operate karte hain, kabhi memory addresses par nahi. Endianness tabhi aati hai jab woh value bytes mein lay out hoti hai. Dekho Data Representation.


L5 — Mastery

Recall Solution 5.1

char[] ek array of single bytes hai, koi ek multi-byte scalar nahi, isliye endianness ise bilkul touch nahi karti. Dono machines: 0:48 ('H') 1:69 ('i') 2:00 (NUL). Endianness bytes ko ek wide integer ke andar reorder karti hai; array element order array ke khud se fix hoti hai.

Recall Solution 5.2

Intended (big-endian reader): offset 0 MSB hai ⇒ . Little-endian host, bina conversion ke: offset 0 ko LSB maanta hai ⇒ . Woh 1 padhta hai 256 ki jagah — ek byte-swap ka fark. Sahi handling: ntohs wapas swap karta hai. Yahi everyday reason hai ki network code explicitly convert karta hai.

Recall Solution 5.3

Store: . Har offset ko se weight karke wapas padho: Lekin bilkul ka base-256 expansion hai, isliye . Convention lossless hai jab tak write rule = read rule ho. (Yahi big-endian par bhi laagu hota hai ke saath.)


Level Map

L1 Recognition

L2 Application

L3 Analysis

L4 Synthesis

L5 Mastery

Spot the rule

Place the bytes

Read scrambled memory

Widths pointers protocols

Traps and proofs

Connections

  • Endianness (big vs little) — parent topic; saare rules wahan build hue hain.
  • Memory Addressing — variable-width reads (Problem 4.1) address ordering par rely karti hain.
  • Data Representation — value-to-bytes map .
  • Network Protocols — big-endian wire order, Problems 3.3 aur 5.2.
  • Pointers and Type Punning — Problem 4.2 mein detector cast.
  • Bitwise Operations — Problem 4.3 mein shift/mask construction.