Skip to content

Instantly share code, notes, and snippets.

@shivakar
Created December 30, 2015 13:55
Show Gist options
  • Save shivakar/d0049c0f3fa42773880f to your computer and use it in GitHub Desktop.
Save shivakar/d0049c0f3fa42773880f to your computer and use it in GitHub Desktop.
Union Trick - Dynamic value interpretation
package main
import (
"bytes"
"encoding/binary"
"fmt"
"math"
)
func main() {
x := uint64(4600000000000000000)
fmt.Println(x)
fmt.Println(math.Float64frombits(x))
}
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
// Bit-wise conversion of an int type to a floating point type.
int main(int argc, char const *argv[]) {
union {
uint64_t int_val;
double double_val;
} conv;
conv.int_val = 4600000000000000000;
printf("Int Val: %"PRIu64"\n", conv.int_val);
printf("Double Val: %g\n", conv.double_val);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment