-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
right_rotate_bits.jl
39 lines (32 loc) · 933 Bytes
/
right_rotate_bits.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Julia program to Right Rotate a number by a specific bits.
# Right Rotate 'cnt' number of bits of the given number 'n'
function right_rotate_bits(n, cnt)
cnt = cnt % 31
while(cnt > 0)
# Store the current LSB in a temporary variable
lsb = n & 1
# Right rotate the given number by one bit and drop its LSB
n = (n >> 1) & (~(1 << 31))
# Set the dropped LSB as the new MSB
n = n | (lsb << 31)
# Decrement cnt
cnt = cnt - 1
end
return n
end
print("Enter the number? ")
num = readline()
num = parse(Int, num)
print("How many bits do you want to rotate? ")
cnt = readline()
cnt = parse(Int, cnt)
left = right_rotate_bits(num, cnt)
print("The Right-rotated number is: $left")
"""
Time Complexity: O(n)
Space Complexity: O(1)
SAMPLE INPUT AND OUTPUT
Enter the number? 39
How many bits do you want to rotate? 17
The Right-rotated number is: 1277952
"""