GoCollections/lang/Number.go

35 lines
907 B
Go
Raw Normal View History

2022-11-29 02:11:39 +08:00
/*
* Copyright (c) 2022 tursom. All rights reserved.
* Use of this source code is governed by a GPL-3
* license that can be found in the LICENSE file.
*/
package lang
// Number interface for real number
type Number interface {
ToInt64() Int64
ToUInt64() UInt64
ToFloat64() Float64
}
2022-12-03 19:13:29 +08:00
2023-04-16 22:37:25 +08:00
func GetBit[T int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 |
Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64](p T, index uint) (ok bool) {
location := T(1) << index
return p&location != 0
}
2023-04-16 14:57:20 +08:00
func SwapBit[T int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 |
2023-04-16 22:37:25 +08:00
Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64](p *T, index uint, new bool) (old bool) {
location := T(1) << index
2022-12-03 19:13:29 +08:00
oldValue := *p
var newValue T
if new {
newValue = oldValue | location
} else {
newValue = oldValue & ^location
}
*p = newValue
2023-04-16 14:57:20 +08:00
return oldValue&location != 0
2022-12-03 19:13:29 +08:00
}