Skip to content
This repository was archived by the owner on Jul 4, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
./.idea
/.idea
24 changes: 24 additions & 0 deletions binary_routines/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ func TestBit(num1 interface{}, bit uint8) (bool, error) {
}
}

// MergeInt8Numbers Combines two unsigned 8-bit numbers into 16-bit unsigned number
// The first number occupies the first 8 bits while the other occupies the rest
func MergeInt8Numbers(num1, num2 uint8) uint16 {
result := uint16(num1) & 0xF << 8
fmt.Printf("First Chunk - %016b\n", result)
result |= uint16(num2) & 0xF
fmt.Printf("Final Chunk - %016b\n", result)

return result
}

// MergeIPAddress Merge Parts of an IP address into a single 32bit number
func MergeIPAddress(part1, part2, part3, part4 uint8) uint32 {
ipAddress := uint32(part1) & 0xFF << 24
fmt.Printf("After First Chunk Added- %032b\n", ipAddress)
ipAddress |= uint32(part2) & 0xFF << 16
fmt.Printf("After Second Chunk Added- %032b\n", ipAddress)
ipAddress |= uint32(part3) & 0xFF << 8
fmt.Printf("After Third Chunk Added- %032b\n", ipAddress)
ipAddress |= uint32(part4) & 0xFF
fmt.Printf("After Final Chunk Added- %032b\n", ipAddress)
return ipAddress
}

// Variadic OR takes a variadic argument of a number of integers, performs a logical OR against all
// of those integers and returns the result
func VariadicOr(input ...int) int {
Expand Down
60 changes: 60 additions & 0 deletions binary_routines/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,63 @@ func TestTestBit(t *testing.T) {
}
})
}

func TestMergeInt8Numbers(t *testing.T) {
type args struct {
num1 uint8
num2 uint8
}
tests := []struct {
name string
args args
want string
}{
{
name: "Test 1",
args: args{
num1: 10,
num2: 10,
},
want: "0000101000001010",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MergeInt8Numbers(tt.args.num1, tt.args.num2); fmt.Sprintf("%016b", got) != tt.want {
t.Errorf("MergeInt8Numbers() = %v, want %v", got, tt.want)
}
})
}
}

func TestMergeIPAddress(t *testing.T) {
type args struct {
part1 uint8
part2 uint8
part3 uint8
part4 uint8
}
tests := []struct {
name string
args args
want uint32
}{
{
name: "127.0.0.1",
args: args{
part1: 127,
part2: 0,
part3: 0,
part4: 1,
},
want: 0x7f000001,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MergeIPAddress(tt.args.part1, tt.args.part2, tt.args.part3, tt.args.part4); got != tt.want {
t.Errorf("MergeIPAddress() = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go-bit-routines

go 1.19