Skip to content

Commit

Permalink
merge: Feat: bits counter (TheAlgorithms#439)
Browse files Browse the repository at this point in the history
* feat: bitwise min

* fix: bitwise min, change for vararg

* fix: change min tests

* fix: benchmark for bitwise

* fix: rename tests

* fix: add value param

* fix: change condition

* feat: bits counter

* Updated Documentation in README.md

* fix: change test cases name

Co-authored-by: Rak Laptudirm <[email protected]>
Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 17, 2021
1 parent e9ba9c4 commit 9fec515
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
---
##### Functions:

1. [`IsPowerOfTwo`](./math/binary/checkisnumberpoweroftwo.go#L19): IsPowerOfTwo This function uses the fact that powers of 2 are represented like 10...0 in binary, and numbers one less than the power of 2 are represented like 11...1. Therefore, using the and function: 10...0 & 01...1 00...0 -> 0 This is also true for 0, which is not a power of 2, for which we have to add and extra condition.
2. [`IsPowerOfTwoLeftShift`](./math/binary/checkisnumberpoweroftwo.go#L26): IsPowerOfTwoLeftShift This function takes advantage of the fact that left shifting a number by 1 is equivalent to multiplying by 2. For example, binary 00000001 when shifted by 3 becomes 00001000, which in decimal system is 8 or = 2 * 2 * 2
3. [`MeanUsingAndXor`](./math/binary/arithmeticmean.go#L11): No description provided.
4. [`MeanUsingRightShift`](./math/binary/arithmeticmean.go#L15): No description provided.
5. [`ReverseBits`](./math/binary/reversebits.go#L14): 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.
6. [`XorSearchMissingNumber`](./math/binary/xorsearch.go#L10): No description provided.
1. [`BitCounter`](./math/binary/bitcounter.go#L11): BitCounter - The function returns the number of set bits for an unsigned integer number
2. [`IsPowerOfTwo`](./math/binary/checkisnumberpoweroftwo.go#L19): IsPowerOfTwo This function uses the fact that powers of 2 are represented like 10...0 in binary, and numbers one less than the power of 2 are represented like 11...1. Therefore, using the and function: 10...0 & 01...1 00...0 -> 0 This is also true for 0, which is not a power of 2, for which we have to add and extra condition.
3. [`IsPowerOfTwoLeftShift`](./math/binary/checkisnumberpoweroftwo.go#L26): IsPowerOfTwoLeftShift This function takes advantage of the fact that left shifting a number by 1 is equivalent to multiplying by 2. For example, binary 00000001 when shifted by 3 becomes 00001000, which in decimal system is 8 or = 2 * 2 * 2
4. [`MeanUsingAndXor`](./math/binary/arithmeticmean.go#L11): No description provided.
5. [`MeanUsingRightShift`](./math/binary/arithmeticmean.go#L15): No description provided.
6. [`ReverseBits`](./math/binary/reversebits.go#L14): 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.
7. [`XorSearchMissingNumber`](./math/binary/xorsearch.go#L10): No description provided.

---
</details><details>
Expand Down
20 changes: 20 additions & 0 deletions math/binary/bitcounter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// bitcounter.go
// description: Counts the number of set bits in a number
// details:
// For unsigned integer number N, return the number of bits set to 1 - [Bit numbering](https://en.wikipedia.org/wiki/Bit_numbering)
// author(s) [red_byte](https://github.com/i-redbyte)
// see bitcounter_test.go

package binary

// BitCounter - The function returns the number of set bits for an unsigned integer number
func BitCounter(n uint) int {
counter := 0
for n != 0 {
if n&1 == 1 {
counter++
}
n >>= 1
}
return counter
}
33 changes: 33 additions & 0 deletions math/binary/bitcounter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// bitcounter_test.go
// description: Test for counts the number of set bits in a number
// author(s) [red_byte](https://github.com/i-redbyte)
// see bitcounter.go

package binary

import "testing"

func TestBitCounter(t *testing.T) {
tests := []struct {
name string
number uint
want int
}{
{"Number of bits in a number 0", 0, 0},
{"Number of bits in a number 1", 1, 1},
{"Number of bits in a number 255", 255, 8},
{"Number of bits in a number 7", 7, 3},
{"Number of bits in a number 8", 8, 1},
{"Number of bits in a number 9223372036854775807", 9223372036854775807, 63},
{"Number of bits in a number 2147483647", 2147483647, 31},
{"Number of bits in a number 15", 15, 4},
{"Number of bits in a number 16", 16, 1},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := BitCounter(test.number); got != test.want {
t.Errorf("BitCounter() = %v, want %v", got, test.want)
}
})
}
}

0 comments on commit 9fec515

Please sign in to comment.