aboutsummaryrefslogtreecommitdiff
path: root/bitfield
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-06-28 20:50:58 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-06-28 20:50:58 +0200
commit47e8704be61e07703c247716c7f48d25eb64427b (patch)
treeba1a6422e8c18a139290b85acd985bc55c7cd983 /bitfield
parent6939b4fbc5a1bb69faeed7fcf20ef4a682d2757e (diff)
Don't panic
Diffstat (limited to 'bitfield')
-rw-r--r--bitfield/bitfield.go16
1 files changed, 7 insertions, 9 deletions
diff --git a/bitfield/bitfield.go b/bitfield/bitfield.go
index 2346a2c..da4d4d8 100644
--- a/bitfield/bitfield.go
+++ b/bitfield/bitfield.go
@@ -13,26 +13,24 @@ func New(n int) BitField {
func (b BitField) At(i int) bool {
x := i / 8
y := 7 - uint(i%8)
- if x < 0 || x > len(b) {
- panic("out of range")
+ if x >= 0 && x < len(b) {
+ return (int(b[x]>>y) & 1) == 1
}
- return (int(b[x]>>y) & 1) == 1
+ return false
}
func (b BitField) Set(i int) {
x := i / 8
y := 7 - uint(i%8)
- if x < 0 || x > len(b) {
- panic("out of range")
+ if x >= 0 && x < len(b) {
+ b[x] |= 1 << y
}
- b[x] |= 1 << y
}
func (b BitField) Clear(i int) {
x := i / 8
y := 7 - uint(i%8)
- if x < 0 || x > len(b) {
- panic("out of range")
+ if x >= 0 && x < len(b) {
+ b[x] &= ^(1 << y)
}
- b[x] &= ^(1 << y)
}