How to convert 0.5 to 1056964608 in one shot

In C language, you can convert 0.5 to 1056964608 in one shot.

#include <stdio.h>

int main() {
    float f = 0.5;
    printf("%d\n", *(int *)&f); // => 1056964608
}

(However, the writing method changes depending on the processing system and CPU architecture. The above is an example of MacOS Sierra and clang-900.0.39.2)

How can I write this in Go language? In conclusion, you can do something similar with the type ʻunsafe.Pointer`.

package main

import "fmt"
import "unsafe"

func main() {
    var f float32 = 0.5
    var p unsafe.Pointer
    p = unsafe.Pointer(&f)
    fmt.Printf("%d\n", *(*int32)(p)); // => 1056964608
}

What is useful is that it makes me feel like I'm messing with memory directly.

Recommended Posts

How to convert 0.5 to 1056964608 in one shot
How to adapt multiple machine learning libraries in one shot
How to convert DateTimeField format in Django
How to calculate "xx time" in one shot with Python timedelta
How to develop in Python
How to convert / restore a string with [] in python
How to define Decorator and Decomaker in one function
How to do zero-padding in one line with OpenCV
How to convert floating point numbers to binary numbers in Python
How to embed multiple embeds in one message with Discord.py
[Python] How to do PCA in Python
How to handle session in SQLAlchemy
Convert markdown to PDF in Python
How to use classes in Theano
How to write soberly in pandas
How to collect images in Python
How to update Spyder in Anaconda
How to check ORM behavior in one file with django
How to reflect CSS in Django
How to kill processes in bulk
How to use Mysql in python
How to convert Tensorflow model to Lite
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to convert from .mgz to .nii.gz
How to run TensorFlow 1.0 code in 2.0
How to handle Japanese in Python
How to log in to Docker + NGINX
How to call PyTorch in Julia
How to combine all CSVs in a folder into one CSV
Shell script to build pyenv environment on ubuntu in one shot
How to display legend marks in one with Python 2D plot
How to use calculated columns in CASTable
[Python] How to convert db file to csv
[Introduction to Python] How to use class in Python?
How to suppress display error in matplotlib
How to access environment variables in Python
How to easily convert format from Markdown
How to dynamically define variables in Python
How to do R chartr () in Python
[Python] How to convert a 2D list to a 1D list
How to convert Python to an exe file
How to delete expired sessions in Django
[Itertools.permutations] How to put permutations in Python
How to use Google Test in C
Convert psd file to png in Python
How to implement nested serializer in drf-flex-fields
How to execute commands in jupyter notebook
How to do'git fetch --tags' in GitPython
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to reassign index in pandas dataframe
How to check opencv version in python
How to enable SSL (TLS) in Apache
How to use Anaconda interpreter in PyCharm
How to specify non-check target in Flake8
How to handle consecutive values in MySQL
How to switch python versions in cloud9
How to adjust image contrast in Python