github.com/tunabay/go-bitarray@v1.3.1/buffer_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() {
    14  	buf := bitarray.NewBuffer(32)
    15  	fmt.Println(buf)
    16  	buf.PutBitAt(0, 1)
    17  	buf.PutBitAt(1, 1)
    18  	fmt.Println(buf)
    19  	buf.PutBitArrayAt(8, bitarray.MustParse("1010101"))
    20  	fmt.Println(buf)
    21  	buf.FillBitsAt(16, 4, 1)
    22  	fmt.Println(buf)
    23  	buf.PutBitArrayAt(24, bitarray.MustParse("1111-0000"))
    24  	fmt.Println(buf)
    25  	buf.ToggleBitsAt(24, 8)
    26  	fmt.Println(buf)
    27  
    28  	fmt.Printf("% b\n", buf.BitArray())
    29  
    30  	// Output:
    31  	// 00000000000000000000000000000000
    32  	// 11000000000000000000000000000000
    33  	// 11000000101010100000000000000000
    34  	// 11000000101010101111000000000000
    35  	// 11000000101010101111000011110000
    36  	// 11000000101010101111000000001111
    37  	// 11000000 10101010 11110000 00001111
    38  }
    39  
    40  func ExampleNewBuffer() {
    41  	buf0 := bitarray.NewBuffer(0)
    42  	buf64 := bitarray.NewBuffer(64)
    43  
    44  	fmt.Println(buf0)
    45  	fmt.Println(buf64)
    46  
    47  	// Output:
    48  	// 0000000000000000000000000000000000000000000000000000000000000000
    49  }
    50  
    51  func ExampleNewBufferFromBitArray() {
    52  	ba := bitarray.MustParse("1111-1111 0000-0000 1111-1111")
    53  	buf := bitarray.NewBufferFromBitArray(ba)
    54  
    55  	fmt.Println(buf)
    56  
    57  	// Output:
    58  	// 111111110000000011111111
    59  }
    60  
    61  func ExampleNewBufferFromByteSlice() {
    62  	b := []byte{0b_0000_1111, 0b_1111_0000}
    63  	buf := bitarray.NewBufferFromByteSlice(b)
    64  
    65  	fmt.Printf("b=%08b, buf=% b\n", b, buf)
    66  	buf.PutBitAt(0, 1)
    67  	buf.PutBitAt(15, 1)
    68  	fmt.Printf("b=%08b, buf=% b\n", b, buf)
    69  
    70  	// Output:
    71  	// b=[00001111 11110000], buf=00001111 11110000
    72  	// b=[10001111 11110001], buf=10001111 11110001
    73  }
    74  
    75  func ExampleNewBufferFromByteSlicePartial() {
    76  	b := []byte{0b_0000_1111, 0b_1111_0000}
    77  	buf := bitarray.NewBufferFromByteSlicePartial(b, 4, 6)
    78  
    79  	fmt.Printf("b=%08b, buf=%b\n", b, buf)
    80  	buf.PutBitAt(0, 0)
    81  	buf.PutBitAt(1, 0)
    82  	buf.PutBitAt(5, 0)
    83  	fmt.Printf("b=%08b, buf=%b\n", b, buf)
    84  
    85  	// Output:
    86  	// b=[00001111 11110000], buf=111111
    87  	// b=[00000011 10110000], buf=001110
    88  }
    89  
    90  func ExampleBuffer_Len() {
    91  	buf := bitarray.NewBuffer(4096)
    92  
    93  	fmt.Println(buf.Len())
    94  
    95  	// Output:
    96  	// 4096
    97  }
    98  
    99  func ExampleBuffer_FillBits() {
   100  	buf := bitarray.NewBuffer(12)
   101  
   102  	buf.Slice(5, 10).FillBits(1)
   103  	fmt.Printf("% b\n", buf)
   104  
   105  	// Output:
   106  	// 00000111 1100
   107  }