Kth Byte from Hexadecimal Number
Filed under Qualcomm
Free read 2 of 3Shift the number right by (k * 8) bits to bring the target byte to the least significant position.
The question
Given a 32-bit hexadecimal number, write a function to extract the k-th byte (0-indexed, where 0 is the least significant byte).
Constraints
- k is between 0 and 3
- Input is a 32-bit unsigned integer
Examples
- Input
- num = 0x12345678, k = 1
- Output
- 0x56
- Why
- The bytes are 0x78 (0), 0x56 (1), 0x34 (2), 0x12 (3).
Approach
Shift the number right by (k * 8) bits to bring the target byte to the least significant position.
Perform a bitwise AND with 0xFF to isolate the target byte.
Return the resulting value.
Solution
O(1) time, O(1) space
def get_kth_byte(num, k):
# Shift right by k*8 bits and mask with 0xFF
return (num >> (k * 8)) & 0xFF
Tips
- What they are testing: Test with k=0 and k=3 to ensure boundary conditions are handled.
- Common mistake: Using incorrect bit-shift amounts or failing to mask the result with 0xFF.
Related Qualcomm questions
Collected from public candidate posts and coding forums. Updated Nov 2026.