Created
December 30, 2015 13:55
-
-
Save shivakar/d0049c0f3fa42773880f to your computer and use it in GitHub Desktop.
Union Trick - Dynamic value interpretation
This file contains 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 ( | |
"bytes" | |
"encoding/binary" | |
"fmt" | |
"math" | |
) | |
func main() { | |
x := uint64(4600000000000000000) | |
fmt.Println(x) | |
fmt.Println(math.Float64frombits(x)) | |
} |
This file contains 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
#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