8000 rotate bits of a number · developgo/algorithms_with_Go@4237b31 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 4237b31

Browse files
committed
rotate bits of a number
1 parent 9e93788 commit 4237b31

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ WIP, the descriptions of the below `unsolved yet` problems can be found in the [
9898
- [] Find the element that appears once
9999
- [x] [Binary representation of a given number](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/decimal_to_binary)
100100
- [] Count total set bits in all numbers from 1 to n
101-
- [] Rotate bits of a number
101+
- [x] [Rotate bits of a number](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/rotate_bits)
102102
- [] Count number of bits to be flipped to convert A to B
103103
- [] Find Next Sparse Number
104104

bitwise/rotate_bits/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Rotate bits of a number
2+
3+
Source: [GeeksforGeeks](https://www.geeksforgeeks.org/rotate-bits-of-an-integer/amp/)
4+
5+
Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.
6+
7+
* In left rotation, the bits that fall off at left end are put back at right end.
8+
* In right rotation, the bits that fall off at right end are put back at left end.
9+
10+
## Example
11+
12+
Let n = 3 is stored using 8 bits.
13+
14+
* Left rotation of n = 11100101 by 3 makes n = 00101111
15+
* Right rotation of n = 11100101 by 3 makes n = 10111100
16+
17+
## Algorithm
18+
19+
Left Rotation:
20+
21+
* n << rotate , leaves last "rotate" bits 0
22+
* to populate these with the first "rotate" bits of the original number
23+
* move first bits of the number to the end (n >> (INT_BITS - rotate))
24+
* and do bitwise OR with the rotated number
25+
26+
Right Rotation:
27+
28+
* n >> rotate , leaves first "rotate" bits 0
29+
* to populate these with the last "rotate" bits of the original number
30+
* move last bits of the number to the begining (n << (INT_BITS - rotate))
31+
* and do bitwise OR with the rotated number
32+
33+
## Result
34+
35+
```bash
36+
$ go run main.go
37+
The number 152 in bits: 10011000
38+
After left rotation the number is 196 and in bits: 11000100
39+
After right rotation the number is 19 and in bits: 10011
40+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// INT_BITS the number n is stored using 8 bits
6+
const INT_BITS = 8
7+
8+
// function to left rotate n by rotate bits
9+
func rotateLeft(n int, rotate int) uint8 {
10+
// n << rotate , leaves last "rotate" bits 0
11+
// to populate these with the first "rotate" bits of the original number
12+
// move first bits of the number to the end (n >> (INT_BITS - rotate))
13+
// and do bitwise OR with the rotated number
14+
return uint8((n << rotate) | (n >> (INT_BITS - rotate)))
15+
}
16+
17+
// function to right rotate n by rotate bits
18+
func rotateRight(n int, rotate int) uint8 {
19+
// n >> rotate , leaves first "rotate" bits 0
20+
// to populate these with the last "rotate" bits of the original number
21+
// move last bits of the number to the begining (n << (INT_BITS - rotate))
22+
// and do bitwise OR with the rotated number
23+
return uint8((n >> rotate) | (n << (INT_BITS - rotate)))
24+
}
25+
26+
func main() {
27+
n := 152
28+
rotate := 3
29+
30+
fmt.Printf("The number %d in bits: %b\n", n, n)
31+
32+
rl := rotateLeft(n, rotate)
33+
fmt.Printf("After left rotation the number is %d and in bits: %b\n", rl, rl)
34+
35+
rr := rotateRight(n, rotate)
36+
fmt.Printf("After right rotation the number is %d and in bits: %b\n", rr, rr)
37+
}
0