Skip to content

Commit

Permalink
extend bitmask sub-package
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonWaldherr committed Jul 22, 2024
1 parent 41d8f56 commit 9489af9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 8 deletions.
50 changes: 47 additions & 3 deletions bitmask/bitmask.go
Original file line number Diff line number Diff line change
@@ -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}
}
Expand Down Expand Up @@ -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}
}
56 changes: 51 additions & 5 deletions bitmask/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,71 @@ 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
// 11110110
// 11110111
// 2: true
// []byte: [11110111]
// 11110110
// 11110111
// Count: 7
// 0
// 10101010
// AND: 10100000
// OR: 11111010
// XOR: 1011010
}

0 comments on commit 9489af9

Please sign in to comment.