Skip to content

Instantly share code, notes, and snippets.

@anisbhsl
Created November 4, 2019 05:24
Show Gist options
  • Save anisbhsl/eb8ed4a2245918a3bccf0d688dc479da to your computer and use it in GitHub Desktop.
Save anisbhsl/eb8ed4a2245918a3bccf0d688dc479da to your computer and use it in GitHub Desktop.
Orders floating point numbers in byte order
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