cuelang.org/go@v0.10.1/pkg/math/bits/manual.go (about)

     1  // Copyright 2018 The CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Copyright 2018 The Go Authors. All rights reserved.
    16  // Use of this source code is governed by a BSD-style
    17  // license that can be found in the LICENSE file.
    18  
    19  package bits
    20  
    21  import (
    22  	"fmt"
    23  	"math"
    24  	"math/big"
    25  	"math/bits"
    26  )
    27  
    28  // Lsh returns x shifted left by n bits.
    29  func Lsh(x *big.Int, n uint) *big.Int {
    30  	var z big.Int
    31  	z.Lsh(x, n)
    32  	return &z
    33  }
    34  
    35  // Rsh returns x shifted right by n bits.
    36  func Rsh(x *big.Int, n uint) *big.Int {
    37  	var z big.Int
    38  	z.Rsh(x, n)
    39  	return &z
    40  }
    41  
    42  // At returns the value of the i'th bit of x.
    43  func At(x *big.Int, i uint) (uint, error) {
    44  	if i > math.MaxInt32 {
    45  		return 0, fmt.Errorf("bit index too large")
    46  	}
    47  	return x.Bit(int(i)), nil
    48  }
    49  
    50  // SetBit returns x with x's i'th bit set to b (0 or 1). That is, if b is 1
    51  // SetBit returns x with its i'th bit set; if b is 0 SetBit returns x with
    52  // its i'th bit cleared.
    53  func Set(x *big.Int, i int, bit uint) *big.Int {
    54  	var z big.Int
    55  	z.SetBit(x, i, bit)
    56  	return &z
    57  }
    58  
    59  // And returns the bitwise and of a and b.
    60  func And(a, b *big.Int) *big.Int {
    61  	var z big.Int
    62  	z.And(a, b)
    63  	return &z
    64  }
    65  
    66  // Or returns the bitwise or of a and b (a | b in Go).
    67  func Or(a, b *big.Int) *big.Int {
    68  	var z big.Int
    69  	z.Or(a, b)
    70  	return &z
    71  }
    72  
    73  // Xor returns the bitwise xor of a and b (a ^ b in Go).
    74  func Xor(a, b *big.Int) *big.Int {
    75  	var z big.Int
    76  	z.Xor(a, b)
    77  	return &z
    78  }
    79  
    80  // Clear returns the bitwise and not of a and b (a &^ b in Go).
    81  func Clear(a, b *big.Int) *big.Int {
    82  	var z big.Int
    83  	z.AndNot(a, b)
    84  	return &z
    85  }
    86  
    87  // OnesCount returns the number of one bits ("population count") in x.
    88  func OnesCount(x *big.Int) int {
    89  	var count int
    90  	for _, w := range x.Bits() {
    91  		count += bits.OnesCount64(uint64(w))
    92  	}
    93  	return count
    94  }
    95  
    96  // TODO: Reverse, ReverseBytes?
    97  // Not entirely sure what that means for infinite precision.
    98  // Reverse returns the value of x with its bits in reversed order.
    99  // func Reverse(x uint64) uint64 {
   100  // 	return bits.Reverse64(x)
   101  // }
   102  
   103  // // ReverseBytes returns the value of x with its bytes in reversed order.
   104  // func ReverseBytes(x uint64) uint64 {
   105  // 	return bits.ReverseBytes64(x)
   106  // }
   107  
   108  // Len returns the length of the absolute value of x in bits. The bit length
   109  // of 0 is 0.
   110  func Len(x *big.Int) int {
   111  	return x.BitLen()
   112  }