forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreversebits.go
More file actions
23 lines (21 loc) · 715 Bytes
/
reversebits.go
File metadata and controls
23 lines (21 loc) · 715 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// reversebits.go
// description: Reverse bits
// details:
// Reverse the bits of an integer number
// author(s) [red_byte](https://github.com/i-redbyte)
// see reversebits_test.go
package binary
// ReverseBits This function initialized the result by 0 (all bits 0) and process the given number starting
// from its least significant bit. If the current bit is 1, set the corresponding most significant bit in the result
// and finally move on to the next bit in the input number.
// Repeat this till all its bits are processed.
func ReverseBits(number uint) uint {
result := uint(0)
intSize := 31
for number != 0 {
result += (number & 1) << intSize
number = number >> 1
intSize -= 1
}
return result
}