5.1.11 · D3 · Hardware › Instruction Set Architecture (ISA) › Endianness (big vs little)
Intuition Yeh page kya hai
Parent note ne tumhe rule sikhaya tha: ek multi-byte number ka kaunsa byte lowest memory address par jaata hai. Yeh page har us case ko drill karta hai jo us rule se aa sakta hai — alag-alag value sizes, zeros, sign-bits, strings, mixed-type structs, aur classic exam twists — taaki koi bhi scenario aisa na ho jo tumne pehle worked out na dekha ho.
Shuru karne se pehle, do symbols ka ek reminder jo hum poore time use karte hain.
Definition Do words jo hum baar baar use karenge
MSB — Most Significant Byte : woh byte jo sabse bada weight carry karta hai. 0x12345678 mein woh 0x12 hai (yeh 2 24 se multiply hota hai). Socho "hex digits ka sabse left wala pair".
LSB — Least Significant Byte : woh byte jo sabse chhota weight carry karta hai, yahan 0x78 (yeh 2 0 se multiply hota hai). Socho "hex digits ka sabse right wala pair, ones place".
Ek hex digit = 4 bits, isliye do hex digits = ek byte = 8 bits . Yahi reason hai ki hum values ko hamesha pairs of hex digits mein chop karte hain.
Endianness choti lagti hai, lekin iske baare mein questions alag-alag cells mein aate hain. Neeche har class of case hai. Baad ke worked examples mein har ek tag karta hai ki woh kaunsa cell cover kar raha hai, aur saath mein poori grid fill ho jaati hai.
Cell
Scenario class
Kya tricky hai
C1
Ordinary 4-byte int, dono endians
Baseline placement
C2
2-byte (16-bit) value
Chhota N — index range change hoti hai
C3
8-byte (64-bit) value
Bada N — bookkeeping stress
C4
Zero value 0x00000000
Degenerate: dono endians identical lagte hain
C5
Palindromic bytes jaise 0x12341234? (partial symmetry)
Kaunse bytes actually move karte hain?
C6
Value jisme middle mein 0x00 / leading zeros hon
Fewer bytes padhna; sign-of-location
C7
Negative int (two's complement, high bit set)
Sign bit MSB mein hota hai — woh kahaan jaata hai?
C8
Ek char[] string (scalar nahi)
Endianness apply nahi hoti — yeh trap hai
C9
Memory read karna → value (inverse direction)
Bytes se V reconstruct karo
C10
Cross-endian transmission (network byte order)
Real-world word problem
C11
Type-punning twist : ek 4-byte int ko do 2-byte halves ki tarah padhna
Exam-style; sub-word views
C12
Mixed-type struct memory mein laid out
Har field independently endian-swap hota hai; padding
Ab hum sab walk karenge.
0x0A0B0C0D (32-bit) ko address 200 par store karo, dono endians
Forecast: Aage padhne se pehle, likho ki tumhara kya khayal hai ki address 200 par har convention mein kaunsa byte baithega.
Bytes extract karo. Yeh step kyun? Placement se pehle hume har byte ki significance jaanni chahiye. b k = ⌊ V /25 6 k ⌋ mod 256 use karke:
b 0 = 0D , b 1 = 0C , b 2 = 0B , b 3 = 0A
Little-endian, mem [ i ] = b i . Kyun? LSB-first matlab ones-byte lowest address par jaata hai, isliye indices seedha copy karte hain:
200 : 0D , 201 : 0C , 202 : 0B , 203 : 0A
Big-endian, mem [ i ] = b 3 − i . Kyun? MSB-first index ko reverse karta hai taaki sabse bada byte sabse chhote address par aaye:
200 : 0A , 201 : 0B , 202 : 0C , 203 : 0D
Verify: big-endian row left-to-right exactly number 0A 0B 0C 0D ki tarah padhti hai, aur little-endian row uska exact reverse hai. ✓ Figure s01 dekho — wahi chaar coloured tiles, address slots mein opposite directions mein daali gayi hain.
Worked example C2 — 16-bit value
0xBEEF ko address 10 par store karo
Forecast: har endian ke liye address 10 par byte guess karo.
Ab N = 2 hai , isliye sirf indices k = 0 , 1 exist karte hain. Yeh step kyun? Formula same hai, lekin index range shrink ho jaati hai — ek common slip yeh hai ki log phir bhi chaar bytes expect karte hain. b 0 = EF , b 1 = BE .
Little-endian: 10 : EF , 11 : BE .
Big-endian: mem [ i ] = b N − 1 − i = b 1 − i → 10 : BE , 11 : EF .
Verify: little-endian reconstruct karo: EF ⋅ 25 6 0 + BE ⋅ 25 6 1 = 239 + 190 ⋅ 256 = 48879 = 0xBEEF . ✓ Figure s02 yeh do-tile flip dikhata hai.
Worked example C3 — 64-bit value
0x1122334455667788 ko address 0 par store karo
Forecast: little-endian mein address 7 par kaunsa byte baithega?
N = 8 , indices k = 0..7 . b 0 = 88 , b 1 = 77 , b 2 = 66 , b 3 = 55 , b 4 = 44 , b 5 = 33 , b 6 = 22 , b 7 = 11 . Kyun? Same extraction, bas aath pairs of hex digits.
Little-endian (mem [ i ] = b i ): 0 : 88 , 1 : 77 , 2 : 66 , 3 : 55 , 4 : 44 , 5 : 33 , 6 : 22 , 7 : 11 .
Big-endian (mem [ i ] = b 7 − i ): 0 : 11 , 1 : 22 , … , 7 : 88 .
Verify: little-endian address 7 par b 7 = 0x11 = MSB hai — exactly wahi jo "highest address biggest byte rakhta hai" little-endian ke liye predict karta hai. ✓ Figure s03 saare aath tiles dono taraf line up karta hai.
Worked example C4 — Degenerate case:
0x00000000 store karo
Forecast: kya dono endians yahan differ karte hain?
Saare bytes 0x00 hain: b 0 = b 1 = b 2 = b 3 = 00 . Yeh step kyun? Jab har byte equal ho, tab unka koi bhi permutation identical hoga.
Little-endian: 00 00 00 00. Big-endian: 00 00 00 00.
Verify: memory images byte-for-byte equal hain → zero par endianness invisible hoti hai (aur kisi bhi value par jiske saare bytes equal hon, jaise 0xFFFFFFFF). Yeh "dono identical lagte hain" wala degenerate cell hai. ✓
Worked example C5 — Partial symmetry:
0x12341234 store karo
Forecast: kaunse bytes actually dono layouts ke beech position change karte hain?
Bytes: b 0 = 34 , b 1 = 12 , b 2 = 34 , b 3 = 12 . Kyun? Note karo ki pattern har do bytes pe repeat hota hai.
Little-endian: 34 12 34 12 . Big-endian: 12 34 12 34 .
Verify: dono layouts equal nahi hain (C4 ke unlike) — 16-bit half ki repetition poori symmetry nahi hai jo chahiye. Full byte-palindromes jaise 0x12211221? Check karo: bytes 21 , 12 , 21 , 12 ; LE = 21 12 21 12, BE = 12 21 12 21 — phir bhi different. Sirf all-equal bytes se identical layouts milti hain. ✓
Worked example C6 — Leading zeros:
0x00000005 ko 100 par store karo, phir 1 byte padho
Forecast: little-endian mein, 100 par ek single byte padhne se true value 5 milegi?
Bytes: b 0 = 05 , b 1 = b 2 = b 3 = 00 .
Little-endian memory: 100 : 05 , 101 : 00 , 102 : 00 , 103 : 00 . Ek byte 100 par padhne se 05 milta hai. Yeh step kyun? Ones-byte lowest address par rehta hai, isliye narrow read mein already low-order data hoti hai — 8→32 bits widening "free" hai.
Big-endian memory: 100 : 00 , 101 : 00 , 102 : 00 , 103 : 05 . 100 par ek byte 00 deta hai — tumhe address 103 padhna padega.
Verify: little-endian single-byte read = 0x05 = 5 = value. ✓ Figure s04 har layout mein single nonzero tile ki location highlight karta hai. Yeh low-address-is-low-order ka practical payoff hai.
Worked example C7 — Signed 32-bit int
-2 store karo (two's complement)
Forecast: har endian mein sign bit kahaan end up hoga?
Bit pattern dhundho. Yeh step kyun? Two's complement − 2 ko 2 32 − 2 = 0xFFFFFFFE represent karta hai. Sign bit MSB ka top bit hai (0xFF, bit pattern 1111 1111).
Bytes: b 0 = FE , b 1 = FF , b 2 = FF , b 3 = FF .
Little-endian: FE FF FF FF — sign carry karne wala MSB highest address par baithta hai.
Big-endian: FF FF FF FE — sign carry karne wala MSB lowest address par baithta hai.
Verify: big-endian ko unsigned pattern ki tarah reconstruct karo: FF ⋅ 25 6 3 + FF ⋅ 25 6 2 + FF ⋅ 256 + FE = 4294967294 = 2 32 − 2 , jo signed 32-bit mein − 2 hai. ✓ Endianness sign interpretation ko kabhi touch nahi karta — woh sirf sign bit wale byte ko relocate karta hai.
Worked example C8 — String
char s[4] = {'H','e','l','o'} store karo
Forecast: kya endianness swap karne se letters reorder ho jaate hain?
Pahchano ki yeh bytes ka array hai, ek wide scalar nahi. Yeh step kyun? Endianness sirf ek single multi-byte number ko split karta hai. Har char already exactly ek byte hai, isliye split karne ko kuch hai hi nahi.
Dono big- aur little-endian machines par: A : ’H’ , A + 1 : ’e’ , A + 2 : ’l’ , A + 3 : ’o’ .
Verify: ASCII codes H=0x48,e=0x65,l=0x6C,o=0x6F endianness se independent same address order mein appear karte hain. ✓ (Trap yeh hai: log little-endian par 'o','l','e','H' expect karte hain — galat; array element order fixed hoti hai.)
Worked example C9 — Memory se value wapas padhna
Forecast: little-endian bytes 40:AB, 41:CD, 42:34, 43:12 — yeh kaunsi 32-bit value hai?
Har byte ko 25 6 offset se weight do kyunki little-endian mein offset byte index k ke barabar hota hai. Yeh step kyun? Hum placement rule mem [ i ] = b i ko invert kar rahe hain.
V = AB ⋅ 25 6 0 + CD ⋅ 25 6 1 + 34 ⋅ 25 6 2 + 12 ⋅ 25 6 3
Compute karo: 171 + 205 ⋅ 256 + 52 ⋅ 65536 + 18 ⋅ 16777216 = 171 + 52480 + 3407872 + 301989888 = 305450411 .
Verify: 305450411 = 0x1234CDAB . ✓ Note karo ki reconstructed hex highest offset (43) ke saath leftmost digits contribute karte hue padhta hai — yeh little-endian reversal kaam kar rahi hai.
Worked example C10 — Network par ek port number bhejna
Forecast: ek little-endian laptop ko TCP port 443 ek 16-bit field mein bhejna hai. Memory mein port 443 hai mem: bb 01. Wire par kaunse do bytes jaayenge, aur naive send kya transmit karega?
443 hex mein 0x01BB hai, isliye b 0 = BB , b 1 = 01 . Laptop ki little-endian memory mein: mem[0]=BB, mem[1]=01. Yeh step kyun? Conversion se pehle hume native layout chahiye.
Network byte order big-endian hai. Protocol MSB first require karta hai, yani wire par 01 BB. Kyun? Dono ends big-endian par agree kar lete hain taaki kisi ko bhi sender ki CPU guess na karni pade.
Naive send(&port, 2) raw memory blast karta hai = BB 01. Receiver big-endian mein padhta hai: BB ⋅ 256 + 01 = 47873 — galat port. Fix hai htons() jo byte-swap karke 01 BB banata hai.
Verify: correct wire bytes 01 BB big-endian mein decode hote hain 01 ⋅ 256 + BB = 256 + 187 = 443 . ✓ Naive BB 01 decode hota hai 0xBB01 = 47873 = 443 . ✓ htons/htonl ke liye Network Protocols dekho.
Worked example C11 — Ek 4-byte int ko do 2-byte halves ki tarah padhna
Forecast: little-endian machine par, uint32_t x = 0xAABBCCDD ko ek pointer ke through do uint16_t halves h[0], h[1] ki tarah reinterpret kiya jaata hai. Woh kya hain?
Memory mein bytes daalo (little-endian). Bytes b 0 = D D , b 1 = C C , b 2 = B B , b 3 = AA → 100:DD, 101:CC, 102:BB, 103:AA. Yeh step kyun? Type-punning physically jo bytes hain woh padhti hai, isliye pehle hume true layout chahiye.
h[0] 100–101 par bytes ko 16-bit little-endian value ki tarah padhta hai: DD + CC ⋅ 256 = 0xCCDD . Kyun? Same LSB-first rule half width par.
h[1] 102–103 par bytes padhta hai: BB + AA ⋅ 256 = 0xAABB .
Verify: 0xCCDD = 52445 aur 0xAABB = 43707 ; aur reassemble karo h [ 0 ] + h [ 1 ] ⋅ 2 16 = 52445 + 43707 ⋅ 65536 = 2864434397 = 0xAABBCCDD = original x . ✓ Big-endian machine par same trick deta hai h [ 0 ] = 0xAABB , h [ 1 ] = 0xCCDD — swapped — jo exactly isliye hai ki type-punning code endianness-dependent hota hai. Pointers and Type Punning dekho.
Worked example C12 — Alag-alag widths ke fields wala struct
Forecast: struct { uint8_t a; uint16_t b; uint32_t c; } mein a=0x11, b=0x2233, c=0x44556677 ke saath, guess karo ki har field endianness ke under kitne bytes swap karta hai.
Alignment ke saath offsets assign karo. Yeh step kyun? uint16_t ko even offset par rehna chahiye aur uint32_t ko 4 ke multiple par, isliye compiler padding insert karta hai. Layout: offset 0 = a, offset 1 = pad , offsets 2–3 = b, offsets 4–7 = c. Total size 8.
Har field independently swap hota hai, sirf apne bytes ke andar. Kyun? Endianness har multi-byte scalar par alag act karta hai; a byte aur pad byte kabhi nahi move karte.
Little-endian: 0:11, 1:pad, 2:33, 3:22, 4:77, 5:66, 6:55, 7:44.
Big-endian: 0:11, 1:pad, 2:22, 3:33, 4:44, 5:55, 6:66, 7:77.
Field a (1 byte) kabhi nahi badalti; b apne 2 bytes reverse karta hai; c apne 4 bytes reverse karta hai. Padding dono mein same rahti hai.
Verify: b ko little-endian bytes 2–3 se reconstruct karo: 33 + 22 ⋅ 256 = 0x2233 = 8755 . c ko little-endian bytes 4–7 se reconstruct karo: 77 + 66 ⋅ 256 + 55 ⋅ 25 6 2 + 44 ⋅ 25 6 3 = 0x44556677 = 1146447479 . ✓ Figure s05 byte grid ko padding shaded aur har field ke arrows alag-alag swap karte hue dikhata hai.
Recall Kya humne matrix ka har cell fill kiya?
C1 baseline ::: C1 example (0x0A0B0C0D)
C2 16-bit ::: C2 example (0xBEEF)
C3 64-bit ::: C3 example (0x11..88)
C4 zero degenerate ::: C4 example (0x00000000)
C5 partial symmetry ::: C5 example (0x12341234)
C6 leading zeros / narrow read ::: C6 example (0x00000005)
C7 negative / sign bit ::: C7 example (-2 = 0xFFFFFFFE)
C8 string not scalar ::: C8 example ("Helo")
C9 read back → value ::: C9 example (0x1234CDAB)
C10 network transmission ::: C10 example (port 443)
C11 sub-word type-pun ::: C11 example (0xAABBCCDD halves)
C12 mixed-type struct ::: C12 example (a/b/c with padding)
Mnemonic Ek line jo sab kuch saath le chale
"Little end leads, big end begins." Little-endian: LSB low address par (leads); big-endian: MSB beginning par. Upar ke har example mein wahi rule hai plus b k = ⌊ V /25 6 k ⌋ mod 256 ke saath careful byte-extraction.
Endianness — parent topic — woh core rule jise yeh examples exercise karte hain.
Data Representation — V = ∑ b k 25 6 k decomposition jis par har example tika hai.
Memory Addressing — offsets aur low/high addresses jo poore use kiye hain.
Network Protocols — C10 ka big-endian "network byte order" aur htons.
Pointers and Type Punning — C11 ka sub-word reinterpretation aur C12 ka struct layout.
Bitwise Operations — shift/mask jo byte extraction implement karta hai.
Instruction Set Architecture (ISA) — CPU ek endianness commit karta hai.