The Student Room Group

Computer science leftshift algorithm

How is this work? As final variable n is 13.
n <— 2
t <— n LEFTSHIFT 3
t <— t- 1
n <— t- n
Reply 1
n = 2
t = 2 << 3 = 16
--t = 15
n = t - n = 13
Yep, works
Reply 2
Original post by Rianne.-.
How is this work? As final variable n is 13.
n <— 2
t <— n LEFTSHIFT 3
t <— t- 1
n <— t- n

The LEFTSHIFT operation takes the left operand (in your case, n) and multiplies it by 2^x, where x is the right operand.

Thus in your case, n would be multiplied by 2^3, the right operand:
2^3 = 8, and 2 * 8 = 16

Moving to the next line:
t <— t- 1

Subtracting 1 from 16 gives us 15.

Finally, 15 - n = 15 - 2 = 13.
(edited 11 months ago)

Quick Reply

Latest

Trending

Trending