diff --git a/bitmask/bitmask.go b/bitmask/bitmask.go index 3c9182a..84bd065 100644 --- a/bitmask/bitmask.go +++ b/bitmask/bitmask.go @@ -1,13 +1,16 @@ -// bitmasks are a way to store multiple boolean values in a single integer +// Package bitmask provides a way to store multiple boolean values in a single integer. package bitmask -import "fmt" +import ( + "fmt" +) +// Bitmask represents an integer that stores multiple boolean values using bit manipulation. type Bitmask struct { value int } -// New creates a new Bitmask +// New creates a new Bitmask with the given initial value. func New(init int) *Bitmask { return &Bitmask{value: init} } @@ -55,3 +58,44 @@ func (b *Bitmask) String() string { func (b *Bitmask) Byte() []byte { return []byte{byte(b.value)} } + +// FromByte initializes the bitmask from a byte. +func FromByte(b byte) *Bitmask { + return &Bitmask{value: int(b)} +} + +// Toggle toggles the bit at the given position. +func (b *Bitmask) Toggle(pos int) { + b.value ^= (1 << pos) +} + +// Count returns the number of set bits (1s) in the bitmask. +func (b *Bitmask) Count() int { + count := 0 + value := b.value + for value > 0 { + count += value & 1 + value >>= 1 + } + return count +} + +// Reset resets all bits to 0. +func (b *Bitmask) Reset() { + b.value = 0 +} + +// And performs bitwise AND operation with another bitmask. +func (b *Bitmask) And(other *Bitmask) *Bitmask { + return &Bitmask{value: b.value & other.value} +} + +// Or performs bitwise OR operation with another bitmask. +func (b *Bitmask) Or(other *Bitmask) *Bitmask { + return &Bitmask{value: b.value | other.value} +} + +// Xor performs bitwise XOR operation with another bitmask. +func (b *Bitmask) Xor(other *Bitmask) *Bitmask { + return &Bitmask{value: b.value ^ other.value} +} diff --git a/bitmask/example_test.go b/bitmask/example_test.go index 93f9ebc..2655268 100644 --- a/bitmask/example_test.go +++ b/bitmask/example_test.go @@ -7,20 +7,58 @@ import ( ) func Example() { + // Initialize a bitmask with all bits set i := bitmask.New(0b11111111) + // Clear the bit at position 0 i.Set(0, false) - fmt.Println(i) + fmt.Println(i) // Expected output: 11111110 + // Clear the bit at position 3 i.Set(3, false) - fmt.Println(i) + fmt.Println(i) // Expected output: 11110110 + // Set the bit at position 0 i.Set(0, true) - fmt.Println(i) + fmt.Println(i) // Expected output: 11110111 - fmt.Println("2:", i.Get(2)) + // Get the value of the bit at position 2 + fmt.Println("2:", i.Get(2)) // Expected output: 2: true - fmt.Printf("[]byte: %b\n", i.Byte()) + // Print the byte representation of the bitmask + fmt.Printf("[]byte: %08b\n", i.Byte()) // Expected output: []byte: [11110111] + + // Toggle the bit at position 0 + i.Toggle(0) + fmt.Println(i) // Expected output: 11110110 + + // Toggle the bit at position 0 again + i.Toggle(0) + fmt.Println(i) // Expected output: 11110111 + + // Count the number of set bits + fmt.Println("Count:", i.Count()) // Expected output: Count: 7 + + // Reset all bits to 0 + i.Reset() + fmt.Println(i) // Expected output: 0 + + // Initialize bitmask from a byte + b := bitmask.FromByte(0b10101010) + fmt.Println(b) // Expected output: 10101010 + + // Bitwise AND with another bitmask + a := bitmask.New(0b11110000) + andResult := a.And(b) + fmt.Println("AND:", andResult) // Expected output: AND: 10100000 + + // Bitwise OR with another bitmask + orResult := a.Or(b) + fmt.Println("OR:", orResult) // Expected output: OR: 11111010 + + // Bitwise XOR with another bitmask + xorResult := a.Xor(b) + fmt.Println("XOR:", xorResult) // Expected output: XOR: 01011010 // Output: // 11111110 @@ -28,4 +66,12 @@ func Example() { // 11110111 // 2: true // []byte: [11110111] + // 11110110 + // 11110111 + // Count: 7 + // 0 + // 10101010 + // AND: 10100000 + // OR: 11111010 + // XOR: 1011010 }