Skip to content

Instantly share code, notes, and snippets.

@ptflp
Created August 19, 2021 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptflp/ab0ee21f6afa54ce365eab0adc0012dc to your computer and use it in GitHub Desktop.
Save ptflp/ab0ee21f6afa54ce365eab0adc0012dc to your computer and use it in GitHub Desktop.
int64 representation in bytes
package main
import (
"fmt"
"unsafe"
"reflect"
)
func main() {
var n uint64 = 1 << (1 << 6) - 1
fmt.Println(n)
sh := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(&n)),
Len: 1 << 3,
Cap: 1 << 3,
}
sb := *(*[]byte)(unsafe.Pointer(sh))
fmt.Println(sb)
for i, _ := range sb {
if i == 0 {continue}
sb[i] = 0
}
fmt.Println(sb)
*sh = *(*reflect.SliceHeader)(unsafe.Pointer(&sb))
n = *(*uint64)(unsafe.Pointer(sh.Data))
fmt.Println(n)
}
/**
18446744073709551615
[255 255 255 255 255 255 255 255]
[255 0 0 0 0 0 0 0]
255
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment