github.com/f-secure-foundry/tamago@v0.0.0-20220307101044-d73fcdd7f11b/bits/bits.go (about) 1 // https://github.com/f-secure-foundry/tamago 2 // 3 // Copyright (c) F-Secure Corporation 4 // https://foundry.f-secure.com 5 // 6 // Use of this source code is governed by the license 7 // that can be found in the LICENSE file. 8 9 // Package bits provides primitives for bitwise operations on uint32 values. 10 package bits 11 12 // Get returns the pointed value at a specific bit position and with a bitmask 13 // applied. 14 func Get(addr *uint32, pos int, mask int) uint32 { 15 return uint32((int(*addr) >> pos) & mask) 16 } 17 18 // Set modifies the pointed value by setting an individual bit at the position 19 // argument. 20 func Set(addr *uint32, pos int) { 21 *addr |= (1 << pos) 22 } 23 24 // Clear modifies the pointed value by clearing an individual bit at the 25 // position argument. 26 func Clear(addr *uint32, pos int) { 27 *addr &= ^(1 << pos) 28 } 29 30 // SetN modifies the pointed value by setting a value at a specific bit 31 // position and with a bitmask applied. 32 func SetN(addr *uint32, pos int, mask int, val uint32) { 33 *addr = (*addr & (^(uint32(mask) << pos))) | (val << pos) 34 }