forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: Feat: bits counter (TheAlgorithms#439)
* 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
1 parent
e9ba9c4
commit 9fec515
Showing
3 changed files
with
60 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |