Created
November 4, 2019 05:24
-
-
Save anisbhsl/eb8ed4a2245918a3bccf0d688dc479da to your computer and use it in GitHub Desktop.
Orders floating point numbers in byte order
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import( | |
"encoding/binary" | |
//"encoding/binary" | |
"fmt" | |
"math" | |
) | |
func main(){ | |
buf:=make([]byte,8) | |
for i:=-10.00;i<=10.00;i+=1.00{ | |
binary.BigEndian.PutUint64(buf,math.Float64bits(float64(i))) | |
if i<0{ | |
for i,_:=range buf{ | |
buf[i]=^buf[i] | |
} | |
}else { | |
buf[0]=buf[0]^0x80 | |
} | |
fmt.Println(i) | |
fmt.Println(buf) | |
if buf[0]<=127{ | |
for i,_:=range buf{ | |
buf[i]=^buf[i] | |
} | |
num:=binary.BigEndian.Uint64(buf) | |
fmt.Println("decoded",math.Float64frombits(num)) | |
}else{ | |
buf[0]=buf[0]^0x80 | |
num:=binary.BigEndian.Uint64(buf) | |
fmt.Println("decoded",math.Float64frombits(num)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment