github.com/tursom/GoCollections@v0.3.10/lang/Number.go (about)

     1  /*
     2   * Copyright (c) 2022 tursom. All rights reserved.
     3   * Use of this source code is governed by a GPL-3
     4   * license that can be found in the LICENSE file.
     5   */
     6  
     7  package lang
     8  
     9  // Number interface for real number
    10  type Number interface {
    11  	ToInt64() Int64
    12  	ToUInt64() UInt64
    13  	ToFloat64() Float64
    14  }
    15  
    16  func GetBit[T int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 |
    17  	Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64](p T, index uint) (ok bool) {
    18  	location := T(1) << index
    19  	return p&location != 0
    20  }
    21  
    22  func SetBit[T int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 |
    23  	Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64](p *T, index uint, new bool) {
    24  	location := T(1) << index
    25  	if new {
    26  		*p |= location
    27  	} else {
    28  		*p &= ^location
    29  	}
    30  }
    31  
    32  func UpBit[T int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 |
    33  	Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64](p *T, index uint) {
    34  	*p |= T(1) << index
    35  }
    36  
    37  func DownBit[T int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 |
    38  	Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64](p *T, index uint) {
    39  	*p &= ^(T(1) << index)
    40  }
    41  
    42  func SwapBit[T int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 |
    43  	Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64](p *T, index uint, new bool) (old bool) {
    44  	location := T(1) << index
    45  	oldValue := *p
    46  	if new {
    47  		*p = oldValue | location
    48  	} else {
    49  		*p = oldValue & ^location
    50  	}
    51  	return oldValue&location != 0
    52  }