-
golang bitwise XOR and symbols are symbols ^ .
fmt.Printf("0x%X\n", 0xFF^0x55) var a uint8 = 0x55 fmt.Printf("%b\n", ^a)
To limit the type or number of bits, otherwise press int print (print negative).
-
Data storage codec
float32 stored as . 4 bytes, Int16 stored as how two bytes, the data size of side storage?
参考:edgexfoundry/device-sdk-go/pkg/commandvalue.go
Means an io.Reader , Int16 and float32 defines the number of bytes of data storage.
func encodeValue(value interface{}) ([]byte, error) { buf := new(bytes.Buffer) err := binary.Write(buf, binary.LittleEndian, value) return buf.Bytes(), err } func decodeValue(reader io.Reader, value interface{}) error { err := binary.Read(reader, binary.LittleEndian, value) return err } func float32Value(buf []byte) (value float32, err error) { err = decodeValue(bytes.NewReader(buf), &value) return value, err } func int16Value(buf []byte) (value int16, err error) { err = decodeValue(bytes.NewReader(buf), &value) return value, err } func uint16Value(buf []byte) (value uint16, err error) { err = decodeValue(bytes.NewReader(buf), &value) return value, err } func main(){ var ff float32 = 100.5 ffb, err := encodeValue(ff) if err != nil { fmt.Println(err) } else { fmt.Println(ffb) } fmt.Println("------------------") ffn, err := float32Value(ffb) if err != nil { fmt.Println(err) } else { fmt.Println(ffn) } fmt.Println("----------------------------------") var us uint16 = 0xFFF1 usb , err := encodeValue(us) if err != nil { fmt.Println(err) } else { fmt.Println(usb) } fmt.Println("------------------") usn, err := uint16Value(usb) if err != nil { fmt.Println(err) } else { fmt.Printf("0x%X\n", usn) var mask uint16 = 0x1 var maskv uint16 = 0x0 usn = usn & (^mask) | mask fmt.Printf("0x%X\n", usn) usn = usn & (^mask) | maskv fmt.Printf("0x%X\n", usn) } }
-
float data precision conversion
Reference: https://studygolang.com/articles/22284 in floating point comparison
golang supports two floating point float32 and float64 , well-known, involve floating point arithmetic or compare the accuracy encounter problems, according to the specific golang implement IEEE 754 cases given.
By default, float32 accuracy after the decimal . 7 bit, float64 precision of decimal places is 15 bits. (Within the accuracy of the data taken when the floating-point comparison, six out of the range into five pieces The storage case 5 may be the actual storage 5.9 homes, may be 5.99 in).
package main import ( "fmt" ) func main(){ var a float32 = 1.0000001 var b float32 = 1.00000001 var bb float32 = 1.00000005 var bbb float32 = 1.00000006 var c float32 = 1.00000000000001 fmt.Println(a == b) // false fmt.Println(a == bb) // false fmt.Println(b == bb) // true fmt.Println(b == c) // true fmt.Println(bb == c)// true fmt.Println (a == bbb) // true fmt.Println (bb == bbb) // false }
Two decimal places after the decimal point:
func updateFloatPrecision(sf string, pre int, bitSize int) (sfp string, err error){ ff, err := strconv.ParseFloat(sf, bitSize) if err != nil { return sf, err } sfp = strconv.FormatFloat(float64(ff), 'f', pre, bitSize) return sfp, nil } sfp := "-3.1415926" sfp, err := updateFloatPrecision(sfp, 2, 32) if! ERR = nil { fmt.Println (ERR) } the else { fmt.Println (SFP) } or SFP, _: = updateFloatPrecision (SFP, 2 , 32 ) fmt.Prinln (SFP) returns to the original string because the error function, after a successful return to the correct string, realistic habits
-
golang maximum value
math package defines the maximum value. https://golang.google.cn/pkg/math/
Floating-point limit values. Max is the largest finite value representable by the type. SmallestNonzero is the smallest positive, non-zero value representable by the type. const ( MaxFloat32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23 SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23) MaxFloat64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52 SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1/2 ** (1023-1 + 52) ) Integer limit values. const ( MaxInt8 = 1 << 7 - 1 MinInt8 = - 1 << 7 MaxInt16 = 1 << 15 - 1 MinInt16 = - 1 << 15 MaxInt32 = 1 << 31 - 1 MinInt32 = - 1 << 31 MaxInt64 =1 << 63 - 1 MinInt64 = - 1 << 63 MaxUint8 = 1 << 8 - 1 MaxUint16 = 1 << 16 - 1 MaxUint32 = 1 << 32 - 1 MaxUint64 = 1 << 64 - 1 )
Reference: golang maximum unsigned
For uncertain digit uint , the largest unsigned number and -1 on the number of bits is the same. However, whether it is const MAXUINT uint = -1 // Error: negative value , or const MAXUINT uint = uint (-1) // Error: negative value , you can not compile because of the natural conflict of the type and scope. But running, -1 values can indeed be passed to unsigned is how compiles, turn this value is passed to unsigned number it? can
var u uint var v = - 1 u = uint (v)
This will get. Another method is to 0 negated. Difference is 0 after negated, is infinitely more than 1 , then the limited range of uint pick it, the compiler will obviously be considered a loss of accuracy. Approach is to construct a determined number of bits 0 , and then inverted, as follows:
const MaxUint = ^uint(0)
This is two ways to get the maximum number of unsigned.
-
more
reference:
-
https://studygolang.com/articles/22284 golang different type comparison
-
https://studygolang.com/articles/5567 golang maximum number of unsigned