github.com/tunabay/go-bitarray@v1.3.1/buffer_rw_example_test.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_test 6 7 import ( 8 "fmt" 9 10 "github.com/tunabay/go-bitarray" 11 ) 12 13 func ExampleBuffer_BitAt() { 14 ba := bitarray.MustParse("1100-10") 15 buf := bitarray.NewBufferFromBitArray(ba) 16 17 fmt.Println(buf.BitAt(0)) 18 fmt.Println(buf.BitAt(3)) 19 fmt.Println(buf.BitAt(5)) 20 21 // Output: 22 // 1 23 // 0 24 // 0 25 } 26 27 func ExampleBuffer_PutBitAt() { 28 ba := bitarray.MustParse("11110000") 29 buf := bitarray.NewBufferFromBitArray(ba) 30 31 fmt.Println(buf) 32 buf.PutBitAt(0, 0) 33 buf.PutBitAt(1, 1) 34 buf.PutBitAt(7, 1) 35 fmt.Println(buf) 36 37 // Output: 38 // 11110000 39 // 01110001 40 } 41 42 func ExampleBuffer_BitArrayAt() { 43 ba := bitarray.MustParse("1100-1010 0000-1111") 44 buf := bitarray.NewBufferFromBitArray(ba) 45 46 fmt.Println(buf.BitArrayAt(0, 6)) 47 fmt.Println(buf.BitArrayAt(4, 10)) 48 49 // Output: 50 // 110010 51 // 1010000011 52 } 53 54 func ExampleBuffer_PutBitArrayAt() { 55 ba := bitarray.MustParse("1111-0000 0000-1111") 56 buf := bitarray.NewBufferFromBitArray(ba) 57 58 fmt.Println(buf) 59 buf.PutBitArrayAt(2, bitarray.MustParse("0101")) 60 buf.PutBitArrayAt(10, bitarray.MustParse("0000")) 61 buf.PutBitArrayAt(6, bitarray.MustParse("1111")) 62 fmt.Println(buf) 63 64 // Output: 65 // 1111000000001111 66 // 1101011111000011 67 } 68 69 func ExampleBuffer_ByteAt() { 70 ba := bitarray.MustParse("1100-1010 0011-1111") 71 buf := bitarray.NewBufferFromBitArray(ba) 72 73 fmt.Printf("%08b\n", buf.ByteAt(2)) 74 fmt.Printf("%08b\n", buf.ByteAt(6)) 75 76 // Output: 77 // 00101000 78 // 10001111 79 } 80 81 func ExampleBuffer_PutByteAt() { 82 ba := bitarray.MustParse("0000-1010 0011-11") 83 buf := bitarray.NewBufferFromBitArray(ba) 84 85 fmt.Println(buf) 86 buf.PutByteAt(2, 0xff) 87 fmt.Println(buf) 88 buf.PutByteAt(6, 0x00) 89 fmt.Println(buf) 90 91 // Output: 92 // 00001010001111 93 // 00111111111111 94 // 00111100000000 95 } 96 97 func ExampleBuffer_RawBytes() { 98 ba := bitarray.MustParse("1111-0000 1010-1010 1111-1111 11") 99 buf := bitarray.NewBufferFromBitArray(ba) 100 101 // not byte-aligned, copied, 0-padded 102 fmt.Printf("%08b\n", buf.Slice(3, 24).RawBytes()) 103 // byte-aligned, not copied, not 0-padded 104 fmt.Printf("%08b\n", buf.Slice(3, 24).Slice(5, 16).RawBytes()) 105 106 // Output: 107 // [10000101 01010111 11111000] 108 // [10101010 11111111] 109 } 110 111 func ExampleBuffer_Bytes() { 112 ba := bitarray.MustParse("1100-1010 1010-1111 0101-11") 113 buf := bitarray.NewBufferFromBitArray(ba) 114 115 fmt.Printf("%08b\n", buf.Bytes()) 116 fmt.Printf("%08b\n", buf.Slice(4, 20).Bytes()) 117 118 // Output: 119 // [11001010 10101111 01011100] 120 // [10101010 11110101] 121 } 122 123 func ExampleBuffer_BytesAt() { 124 ba := bitarray.MustParse("1100-1010 0011-1111 1010-0000 0111-1000") 125 buf := bitarray.NewBufferFromBitArray(ba) 126 127 fmt.Printf("%08b\n", buf.BytesAt(2, 3)) 128 fmt.Printf("%08b\n", buf.BytesAt(12, 2)) 129 130 // Output: 131 // [00101000 11111110 10000001] 132 // [11111010 00000111] 133 } 134 135 func ExampleBuffer_PutBytesAt() { 136 ba := bitarray.MustParse("1100-1010 0011-1111 1010-0000 0111-1000") 137 buf := bitarray.NewBufferFromBitArray(ba) 138 139 fmt.Println(buf) 140 buf.PutBytesAt(2, []byte{0x00, 0x00, 0x00}) 141 fmt.Println(buf) 142 buf.PutBytesAt(6, []byte{0xAA, 0xFF, 0xAA}) 143 fmt.Println(buf) 144 145 // Output: 146 // 11001010001111111010000001111000 147 // 11000000000000000000000000111000 148 // 11000010101010111111111010101000 149 }