github.com/tunabay/go-bitarray@v1.3.1/buffer_integer_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_Uint8() {
    14  	b := []byte{0b_1100_1010, 0b_1111_0000}
    15  	buf := bitarray.NewBufferFromByteSlice(b)
    16  
    17  	fmt.Println(buf.Slice(4, 12).Uint8()) // 1010 1111
    18  	fmt.Println(buf.Slice(8, 12).Uint8()) // 1111
    19  	fmt.Println(buf.Slice(4, 10).Uint8()) // 1010 11
    20  
    21  	// Output:
    22  	// 175
    23  	// 15
    24  	// 43
    25  }
    26  
    27  func ExampleBuffer_PutUint8() {
    28  	b := make([]byte, 2)
    29  	buf := bitarray.NewBufferFromByteSlice(b)
    30  
    31  	buf.Slice(4, 12).PutUint8(255) // 11111111
    32  	fmt.Printf("%08b\n", b)
    33  	buf.Slice(0, 6).PutUint8(42)   // 101010
    34  	buf.SliceToEnd(13).PutUint8(7) // 111
    35  	fmt.Printf("%08b\n", b)
    36  
    37  	// Output:
    38  	// [00001111 11110000]
    39  	// [10101011 11110111]
    40  }
    41  
    42  func ExampleBuffer_Uint16() {
    43  	b := []byte{0x12, 0x34, 0x56}
    44  	buf := bitarray.NewBufferFromByteSlice(b)
    45  
    46  	fmt.Printf("%04x\n", buf.Slice(4, 20).Uint16())
    47  	fmt.Printf("%04x\n", buf.Slice(8, 20).Uint16())
    48  
    49  	// Output:
    50  	// 2345
    51  	// 0345
    52  }
    53  
    54  func ExampleBuffer_PutUint16() {
    55  	b := make([]byte, 3)
    56  	buf := bitarray.NewBufferFromByteSlice(b)
    57  
    58  	buf.Slice(4, 20).PutUint16(0xffff)
    59  	fmt.Printf("%08b\n", b)
    60  	buf.Slice(10, 16).PutUint16(0b1010)
    61  	fmt.Printf("%08b\n", b)
    62  
    63  	// Output:
    64  	// [00001111 11111111 11110000]
    65  	// [00001111 11001010 11110000]
    66  }
    67  
    68  func ExampleBuffer_Uint32() {
    69  	b := []byte{0x12, 0x34, 0x56, 0x78, 0x9a}
    70  	buf := bitarray.NewBufferFromByteSlice(b)
    71  
    72  	fmt.Printf("%08x\n", buf.Slice(4, 36).Uint32())
    73  	fmt.Printf("%08x\n", buf.Slice(8, 20).Uint32())
    74  
    75  	// Output:
    76  	// 23456789
    77  	// 00000345
    78  }
    79  
    80  func ExampleBuffer_PutUint32() {
    81  	b := make([]byte, 5)
    82  	buf := bitarray.NewBufferFromByteSlice(b)
    83  
    84  	buf.Slice(4, 36).PutUint32(0xff00ffff)
    85  	fmt.Printf("%08b\n", b)
    86  	buf.Slice(16, 28).PutUint32(0xf0c)
    87  	fmt.Printf("%08b\n", b)
    88  
    89  	// Output:
    90  	// [00001111 11110000 00001111 11111111 11110000]
    91  	// [00001111 11110000 11110000 11001111 11110000]
    92  }
    93  
    94  func ExampleBuffer_Uint64() {
    95  	b := []byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12}
    96  	buf := bitarray.NewBufferFromByteSlice(b)
    97  
    98  	fmt.Printf("%016x\n", buf.Slice(4, 68).Uint64())
    99  	fmt.Printf("%016x\n", buf.Slice(12, 24).Uint64())
   100  
   101  	// Output:
   102  	// 23456789abcdef01
   103  	// 0000000000000456
   104  }
   105  
   106  func ExampleBuffer_PutUint64() {
   107  	b := make([]byte, 9)
   108  	buf := bitarray.NewBufferFromByteSlice(b)
   109  
   110  	buf.Slice(4, 68).PutUint64(0x1234567812345678)
   111  	fmt.Printf("%x\n", b)
   112  	buf.Slice(12, 24).PutUint64(0xabc)
   113  	fmt.Printf("%x\n", b)
   114  
   115  	// Output:
   116  	// 012345678123456780
   117  	// 012abc678123456780
   118  }