Go Primer / lab
0 / 57 completed

mybitsGo

UNIT 15 test scopes

*Modelled on math/bits.*

math/bits provides most of its functions in several widths. You write the 32-bit version only, exactly as the standard one.

func OnesCount32(x uint32) int
func Len32(x uint32) int
func RotateLeft32(x uint32, k int) uint32
func Reverse32(x uint32) uint32
func ReverseBytes32(x uint32) uint32

All standard:

YoursStandard
OnesCount32math/bits.OnesCount32
Len32math/bits.Len32
RotateLeft32math/bits.RotateLeft32
Reverse32math/bits.Reverse32
ReverseBytes32math/bits.ReverseBytes32

Things to find out in your tests:

- x & (x-1) clears the lowest set bit of x. Why? Write OnesCount32 again with it. - What does x &^ y do? How does it differ from x & ^y? - Compare the 8, 16, 32 and 64-bit versions in the real package. What changes between them?


solution.go GO
OnesCount32 GoDoc

OnesCount32 counts the bits set to 1 in x. Zero has no set bits.

math/bits.OnesCount32 ↗
package mybits supplied automatically
Saved locallyUTF-8 · tabs

Runs send code + tests to Go Playground.

Test results

Ready when you are

8 automatic test groups for OnesCount32.
Assertions stay hidden until you choose to see them.