github.com/tunabay/go-bitarray@v1.3.1/buffer_integer.go (about) 1 // Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved. 2 // Use of this source code is governed by the MIT license that can be found in 3 // the LICENSE file. 4 5 package bitarray 6 7 import ( 8 "encoding/binary" 9 ) 10 11 // Uint8 returns up to 8 bits from the beginning of the buffer as a uint8 value. 12 // If buf.Len() is greater than 8, only the first 8 bits are used. If buf.Len() 13 // is less than 8, it is treated as an integer with that number of bits. For 14 // example, if buf.Len() == 5, it returns a 5-bit integer, 0 to 31(=0b11111), as 15 // a uint8 value. 16 func (buf *Buffer) Uint8() uint8 { 17 if buf.nBits == 0 { 18 return 0 19 } 20 n, off := 8, 0 21 if buf.nBits < 8 { 22 n, off = buf.nBits, 8-buf.nBits 23 } 24 b := make([]byte, 1) 25 copyBits(b, buf.b, off, buf.off, n) 26 27 return b[0] 28 } 29 30 // PutUint8 sets a uint8 value v within up to 8 bits from the beginning of the 31 // buffer. If buf.Len() is greater than 8, only the first 8 bits are updated. If 32 // buf.Len() is less than 8, only the LSBs of v are written. 33 func (buf *Buffer) PutUint8(v uint8) { 34 if buf.nBits == 0 { 35 return 36 } 37 n, off := 8, 0 38 if buf.nBits < 8 { 39 n, off = buf.nBits, 8-buf.nBits 40 } 41 b := []byte{v} 42 copyBits(buf.b, b, buf.off, off, n) 43 } 44 45 // Uint16 returns up to 16 bits from the beginning of the buffer as a uint16 46 // value. If buf.Len() is greater than 16, only the first 16 bits are used. If 47 // buf.Len() is less than 16, it is treated as an integer with that number of 48 // bits. For example, if buf.Len() == 5, it returns a 5-bit integer, 0 to 49 // 31(=0b11111), as a uint16 value. 50 func (buf *Buffer) Uint16() uint16 { 51 if buf.nBits == 0 { 52 return 0 53 } 54 n, off := 16, 0 55 if buf.nBits < 16 { 56 n, off = buf.nBits, 16-buf.nBits 57 } 58 b := make([]byte, 2) 59 copyBits(b, buf.b, off, buf.off, n) 60 61 return binary.BigEndian.Uint16(b) 62 } 63 64 // PutUint16 sets a uint16 value v within up to 16 bits from the beginning of 65 // the buffer. If buf.Len() is greater than 16, only the first 16 bits are 66 // updated. If buf.Len() is less than 16, only the LSBs of v are written. 67 func (buf *Buffer) PutUint16(v uint16) { 68 if buf.nBits == 0 { 69 return 70 } 71 n, off := 16, 0 72 if buf.nBits < 16 { 73 n, off = buf.nBits, 16-buf.nBits 74 } 75 b := make([]byte, 2) 76 binary.BigEndian.PutUint16(b, v) 77 copyBits(buf.b, b, buf.off, off, n) 78 } 79 80 // Uint32 returns up to 32 bits from the beginning of the buffer as a uint32 81 // value. If buf.Len() is greater than 32, only the first 32 bits are used. If 82 // buf.Len() is less than 32, it is treated as an integer with that number of 83 // bits. For example, if buf.Len() == 5, it returns a 5-bit integer, 0 to 84 // 31(=0b11111), as a uint32 value. 85 func (buf *Buffer) Uint32() uint32 { 86 if buf.nBits == 0 { 87 return 0 88 } 89 n, off := 32, 0 90 if buf.nBits < 32 { 91 n, off = buf.nBits, 32-buf.nBits 92 } 93 b := make([]byte, 4) 94 copyBits(b, buf.b, off, buf.off, n) 95 96 return binary.BigEndian.Uint32(b) 97 } 98 99 // PutUint32 sets a uint32 value v within up to 32 bits from the beginning of 100 // the buffer. If buf.Len() is greater than 32, only the first 32 bits are 101 // updated. If buf.Len() is less than 32, only the LSBs of v are written. 102 func (buf *Buffer) PutUint32(v uint32) { 103 if buf.nBits == 0 { 104 return 105 } 106 n, off := 32, 0 107 if buf.nBits < 32 { 108 n, off = buf.nBits, 32-buf.nBits 109 } 110 b := make([]byte, 4) 111 binary.BigEndian.PutUint32(b, v) 112 copyBits(buf.b, b, buf.off, off, n) 113 } 114 115 // Uint64 returns up to 64 bits from the beginning of the buffer as a uint64 116 // value. If buf.Len() is greater than 64, only the first 64 bits are used. If 117 // buf.Len() is less than 64, it is treated as an integer with that number of 118 // bits. For example, if buf.Len() == 5, it returns a 5-bit integer, 0 to 119 // 31(=0b11111), as a uint64 value. 120 func (buf *Buffer) Uint64() uint64 { 121 if buf.nBits == 0 { 122 return 0 123 } 124 n, off := 64, 0 125 if buf.nBits < 64 { 126 n, off = buf.nBits, 64-buf.nBits 127 } 128 b := make([]byte, 8) 129 copyBits(b, buf.b, off, buf.off, n) 130 131 return binary.BigEndian.Uint64(b) 132 } 133 134 // PutUint64 sets a uint64 value v within up to 64 bits from the beginning of 135 // the buffer. If buf.Len() is greater than 64, only the first 64 bits are 136 // updated. If buf.Len() is less than 64, only the LSBs of v are written. 137 func (buf *Buffer) PutUint64(v uint64) { 138 if buf.nBits == 0 { 139 return 140 } 141 n, off := 64, 0 142 if buf.nBits < 64 { 143 n, off = buf.nBits, 64-buf.nBits 144 } 145 b := make([]byte, 8) 146 binary.BigEndian.PutUint64(b, v) 147 copyBits(buf.b, b, buf.off, off, n) 148 }