Skip to content

Commit 68cabe8

Browse files
committed
decimal to binary
1 parent 01c7d35 commit 68cabe8

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

bitwise/decimal_to_binary/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Binary representation of a given number
2+
3+
Source: [GeeksforGeeks](https://www.geeksforgeeks.org/binary-representation-of-a-given-number/amp/)
4+
Write a program to print Binary representation of a given number.
5+
6+
## Algorithm
7+
8+
Recursive using bitwise operator
9+
10+
* Check n > 1
11+
* **right shift** the number by 1 bit and recursive call the function
12+
* Print the bits of number
13+
14+
Alternate solution is to use fmt Go package to transform decimal to binary:
15+
16+
```go
17+
fmt.Printf("The binary of the %d is: %b\n", x, x)
18+
```

bitwise/decimal_to_binary/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// recursively call this function untill all bits are shifted
6+
func bin(x int) {
7+
8+
if x > 1 {
9+
bin(x >> 1)
10+
}
11+
// print last bit from the remainder
12+
fmt.Printf("%d", x&1)
13+
}
14+
15+
func main() {
16+
x := 131
17+
fmt.Printf("Using recursive function, the binary of %d is: ", x)
18+
bin(x)
19+
fmt.Println()
20+
//builtin using fmt package
21+
fmt.Printf("Using fmt package, the binary of %d is: %b\n", x, x)
22+
}

bitwise/max_xor/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Find the maximum subarray XOR in a given array
22

3+
Source: [GeeksforGeeks](https://www.geeksforgeeks.org/find-the-maximum-subarray-xor-in-a-given-array/amp/)
34
Given an array of integers. find the maximum XOR subarray value in given array. Expected time complexity O(n).
45

56
> Input: arr[] = {1, 2, 3, 4}

0 commit comments

Comments
 (0)