Created
August 19, 2021 07:00
-
-
Save ptflp/ab0ee21f6afa54ce365eab0adc0012dc to your computer and use it in GitHub Desktop.
int64 representation in bytes
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 ( | |
"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