github.com/tunabay/go-bitarray@v1.3.1/buffer_copy_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_CopyBitsFromBytes() {
    14  	ba := bitarray.MustParse("1100-1010 0001-0011 1010-00")
    15  	buf := bitarray.NewBufferFromBitArray(ba)
    16  
    17  	fmt.Println(buf)
    18  	buf.CopyBitsFromBytes(2, []byte{0xff, 0x00}, 6, 4)
    19  	fmt.Println(buf)
    20  	buf.CopyBitsFromBytes(6, []byte{0xAA, 0xFF, 0xAA}, 4, 16)
    21  	fmt.Println(buf)
    22  
    23  	// Output:
    24  	// 1100101000010011101000
    25  	// 1111001000010011101000
    26  	// 1111001010111111111010
    27  }
    28  
    29  func ExampleBuffer_CopyBitsToBytes() {
    30  	ba := bitarray.MustParse("1100-1010 0001")
    31  	buf := bitarray.NewBufferFromBitArray(ba)
    32  
    33  	b := make([]byte, 3)
    34  
    35  	buf.CopyBitsToBytes(0, b, 0, 12)
    36  	fmt.Printf("%08b\n", b)
    37  
    38  	buf.CopyBitsToBytes(4, b, 16, 4)
    39  	buf.CopyBitsToBytes(4, b, 20, 4)
    40  	fmt.Printf("%08b\n", b)
    41  
    42  	// Output:
    43  	// [11001010 00010000 00000000]
    44  	// [11001010 00010000 10101010]
    45  }
    46  
    47  func ExampleCopyBits() {
    48  	ba := bitarray.MustParse("1100-1010 0001")
    49  	bufSrc := bitarray.NewBufferFromBitArray(ba)
    50  	bufDstL := bitarray.NewBuffer(18)
    51  	bufDstS := bitarray.NewBuffer(7)
    52  
    53  	bitarray.CopyBits(bufDstL, bufSrc)
    54  	fmt.Println(bufDstL)
    55  	bitarray.CopyBits(bufDstS, bufSrc)
    56  	fmt.Println(bufDstS)
    57  
    58  	// Output:
    59  	// 110010100001000000
    60  	// 1100101
    61  }
    62  
    63  func ExampleCopyBitsN() {
    64  	ba := bitarray.MustParse("1100-1010 1111")
    65  	bufSrc := bitarray.NewBufferFromBitArray(ba)
    66  	bufDstL := bitarray.NewBuffer(18)
    67  	bufDstS := bitarray.NewBuffer(7)
    68  
    69  	bitarray.CopyBitsN(bufDstL, bufSrc, 10)
    70  	fmt.Println(bufDstL)
    71  	bitarray.CopyBitsN(bufDstS, bufSrc, 10)
    72  	fmt.Println(bufDstS)
    73  
    74  	// Output:
    75  	// 110010101100000000
    76  	// 1100101
    77  }
    78  
    79  func ExampleCopyBitsPartial() {
    80  	ba := bitarray.MustParse("1101-1110 0011")
    81  	bufSrc := bitarray.NewBufferFromBitArray(ba)
    82  	bufDst := bitarray.NewBuffer(18)
    83  
    84  	bitarray.CopyBitsPartial(bufDst, bufSrc, 10, 3, 4)
    85  	fmt.Println(bufDst)
    86  
    87  	// Output:
    88  	// 000000000011110000
    89  }