diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..194cf9c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +./.idea +/.idea \ No newline at end of file diff --git a/binary_routines/binary.go b/binary_routines/binary.go index c5745da..2bd7bba 100644 --- a/binary_routines/binary.go +++ b/binary_routines/binary.go @@ -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 { diff --git a/binary_routines/binary_test.go b/binary_routines/binary_test.go index ca95b08..8340f90 100644 --- a/binary_routines/binary_test.go +++ b/binary_routines/binary_test.go @@ -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) + } + }) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5d5005b --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module go-bit-routines + +go 1.19