github.com/tunabay/go-bitarray@v1.3.1/buffer_bitwise_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_ToggleBitAt() {
    14  	ba := bitarray.MustParse("110010")
    15  	buf := bitarray.NewBufferFromBitArray(ba)
    16  
    17  	fmt.Println(buf)
    18  	buf.ToggleBitAt(1)
    19  	buf.ToggleBitAt(3)
    20  	buf.ToggleBitAt(5)
    21  	fmt.Println(buf)
    22  
    23  	// Output:
    24  	// 110010
    25  	// 100111
    26  }
    27  
    28  func ExampleBuffer_ToggleBitsAt() {
    29  	ba := bitarray.MustParse("11110000")
    30  	buf := bitarray.NewBufferFromBitArray(ba)
    31  
    32  	fmt.Println(buf)
    33  	buf.ToggleBitsAt(2, 4)
    34  	fmt.Println(buf)
    35  
    36  	// Output:
    37  	// 11110000
    38  	// 11001100
    39  }
    40  
    41  func ExampleBuffer_AndAt() {
    42  	ba := bitarray.MustParse("11110000")
    43  	buf := bitarray.NewBufferFromBitArray(ba)
    44  
    45  	fmt.Println(buf)
    46  	buf.AndAt(2, bitarray.MustParse("0110"))
    47  	fmt.Println(buf)
    48  
    49  	// Output:
    50  	// 11110000
    51  	// 11010000
    52  }
    53  
    54  func ExampleBuffer_OrAt() {
    55  	ba := bitarray.MustParse("11110000")
    56  	buf := bitarray.NewBufferFromBitArray(ba)
    57  
    58  	fmt.Println(buf)
    59  	buf.OrAt(2, bitarray.MustParse("0110"))
    60  	fmt.Println(buf)
    61  
    62  	// Output:
    63  	// 11110000
    64  	// 11111000
    65  }
    66  
    67  func ExampleBuffer_XorAt() {
    68  	ba := bitarray.MustParse("11110000")
    69  	buf := bitarray.NewBufferFromBitArray(ba)
    70  
    71  	fmt.Println(buf)
    72  	buf.XorAt(2, bitarray.MustParse("0110"))
    73  	fmt.Println(buf)
    74  
    75  	// Output:
    76  	// 11110000
    77  	// 11101000
    78  }
    79  
    80  func ExampleBuffer_LeadingZeros() {
    81  	ba := bitarray.MustParse("11110000")
    82  	buf := bitarray.NewBufferFromBitArray(ba)
    83  
    84  	fmt.Printf("%b: %d\n", buf, buf.LeadingZeros())
    85  	buf.ToggleBitsAt(0, 2)
    86  	fmt.Printf("%b: %d\n", buf, buf.LeadingZeros())
    87  
    88  	// Output:
    89  	// 11110000: 0
    90  	// 00110000: 2
    91  }
    92  
    93  func ExampleBuffer_TrailingZeros() {
    94  	ba := bitarray.MustParse("11110000")
    95  	buf := bitarray.NewBufferFromBitArray(ba)
    96  
    97  	fmt.Printf("%b: %d\n", buf, buf.TrailingZeros())
    98  	buf.ToggleBitsAt(6, 2)
    99  	fmt.Printf("%b: %d\n", buf, buf.TrailingZeros())
   100  
   101  	// Output:
   102  	// 11110000: 4
   103  	// 11110011: 0
   104  }
   105  
   106  func ExampleBuffer_OnesCount() {
   107  	ba := bitarray.MustParse("00111100")
   108  	buf := bitarray.NewBufferFromBitArray(ba)
   109  
   110  	fmt.Printf("%b: %d\n", buf, buf.OnesCount())
   111  	buf.ToggleBitsAt(0, 6)
   112  	fmt.Printf("%b: %d\n", buf, buf.OnesCount())
   113  
   114  	// Output:
   115  	// 00111100: 4
   116  	// 11000000: 2
   117  }