Problem · Bit Manipulation
Check Endianness Of A Byte Array
Learn this problemProblem statement
You are given an integer array bytes whose values are unsigned 8-bit quantities in [0, 255], and a non-negative integer value.
Interpret bytes as a fixed-width encoding of value using exactly bytes.length bytes.
Return "little" if bytes is the little-endian encoding of value (least significant byte at index 0). Return "big" if bytes is the big-endian encoding of value (most significant byte at index 0). Return "neither" if value does not fit in that many bytes or neither encoding matches.
When both encodings are identical, including every one-byte input, return "little".
Function
checkEndianness(bytes: int[], value: int) → StringExamples
Example 1
bytes = [1,0,0,0]value = 1return = "little"The least significant byte of 1 is 1 and sits at index 0.
Example 2
bytes = [0,0,0,1]value = 1return = "big"The least significant byte of 1 sits at the last index, which is big-endian.
Constraints
1 <= bytes.length <= 4.0 <= bytes[i] <= 255.0 <= value <= 2^31 - 1.