From c82ab66ee573c5b9fd5816e39b81352066e098ee Mon Sep 17 00:00:00 2001 From: oyamo Date: Sun, 19 Feb 2023 15:19:45 +0300 Subject: [PATCH 1/8] add mergeint8numbers function --- .idea/.gitignore | 8 ++++++++ .idea/misc.xml | 6 ++++++ binary_routines/binary.go | 10 ++++++++++ 3 files changed, 24 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7bc123c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/binary_routines/binary.go b/binary_routines/binary.go index c5745da..0d767ae 100644 --- a/binary_routines/binary.go +++ b/binary_routines/binary.go @@ -92,6 +92,16 @@ func TestBit(num1 interface{}, bit uint8) (bool, error) { } } +// MergeInt8Numbers Combines two unsigned 8-bit numbers into 16-bit unsigned number +func MergeInt8Numbers(num1, num2 uint8) uint16 { + result := uint16(num1) << 8 + fmt.Printf("First Chunk - %b", result) + result |= uint16(num2) + fmt.Printf("Final Chunk - %b", result) + + return result +} + // 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 { From 7ec083b2e8121d055694eceefffadf69a7795d72 Mon Sep 17 00:00:00 2001 From: oyamo Date: Sun, 19 Feb 2023 15:20:57 +0300 Subject: [PATCH 2/8] add mergeint8numbers function --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore 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 From ccae4237370f7ef83817f0266d83260069ec73bf Mon Sep 17 00:00:00 2001 From: oyamo Date: Sun, 19 Feb 2023 15:23:17 +0300 Subject: [PATCH 3/8] remove idea file --- .idea/.gitignore | 8 -------- .idea/misc.xml | 6 ------ 2 files changed, 14 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/misc.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 7bc123c..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 7489ef738f8caab9cfc5c9aa1abf1e1a92318aaf Mon Sep 17 00:00:00 2001 From: oyamo Date: Sun, 19 Feb 2023 15:38:52 +0300 Subject: [PATCH 4/8] create a mod file for the tests to run --- go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 go.mod 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 From beaee8e325b0f406c6dfa0f5a842d771cd4128fc Mon Sep 17 00:00:00 2001 From: oyamo Date: Sun, 19 Feb 2023 15:39:34 +0300 Subject: [PATCH 5/8] create a test for mergeint8 function --- binary_routines/binary_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/binary_routines/binary_test.go b/binary_routines/binary_test.go index ca95b08..b62016c 100644 --- a/binary_routines/binary_test.go +++ b/binary_routines/binary_test.go @@ -108,3 +108,31 @@ 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) + } + }) + } +} From 6fdc8b7f8bb96df271b97ac4f79377b0afce5e7e Mon Sep 17 00:00:00 2001 From: oyamo Date: Sun, 19 Feb 2023 15:47:06 +0300 Subject: [PATCH 6/8] log mergeint8numbers function --- binary_routines/binary.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/binary_routines/binary.go b/binary_routines/binary.go index 0d767ae..0e9db28 100644 --- a/binary_routines/binary.go +++ b/binary_routines/binary.go @@ -93,11 +93,12 @@ 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) << 8 - fmt.Printf("First Chunk - %b", result) - result |= uint16(num2) - fmt.Printf("Final Chunk - %b", result) + 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 } From 19be97004238995bea1f8c391250b840c7ec4f3d Mon Sep 17 00:00:00 2001 From: oyamo Date: Sun, 19 Feb 2023 15:54:58 +0300 Subject: [PATCH 7/8] create a test for mergeipaddress function --- binary_routines/binary_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/binary_routines/binary_test.go b/binary_routines/binary_test.go index b62016c..8340f90 100644 --- a/binary_routines/binary_test.go +++ b/binary_routines/binary_test.go @@ -136,3 +136,35 @@ func TestMergeInt8Numbers(t *testing.T) { }) } } + +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) + } + }) + } +} From aad777488fd73dccbf48e8fe94ae4c7a8113210f Mon Sep 17 00:00:00 2001 From: oyamo Date: Sun, 19 Feb 2023 15:55:16 +0300 Subject: [PATCH 8/8] add merge ipaddress function --- binary_routines/binary.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/binary_routines/binary.go b/binary_routines/binary.go index 0e9db28..2bd7bba 100644 --- a/binary_routines/binary.go +++ b/binary_routines/binary.go @@ -103,6 +103,19 @@ func MergeInt8Numbers(num1, num2 uint8) uint16 { 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 {