Computer Architecture
Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.
MSB | LSB | |||
---|---|---|---|---|
left | 1 | 1 | 0 | 0 |
right | 1 | 1 | 1 | 1 |
Concept:
The value changes from least significant bit and stop when two values are same, in the such process the bits never change is the highest consecutive set bits, which means that the result is equal to highest consecutive set bits.
Test:
The result of the input [12, 15] should be 12, since both number have 2 two consecutive set bit in the front.
Explanation:
10-13: load value of left, right into register.
14: clear t0
in order to count shifts.
17: check the equality of left and right to decide whether stop execution or not.
18-19: shift left and right logically for one bit.
20: increment t0
21: jump backward to check if left and right equal now.