julia> hex2bytes(num2hex(1.0))
8-element Array{UInt8,1}:
0x3f
0xf0
0x00
0x00
0x00
0x00
0x00
julia> a = [1.0]1-element Array{Float64,1}: 1.0
julia> b = reinterpret(UInt8, a)8-element Array{UInt8,1}: 0x00 0x00 0x00 0x00 0x00 0x00 0xf0 0x3f
julia> c = reinterpret(Float64, b)1-element Array{Float64,1}: 1.0julia> using Base.Intrinsics
julia> theFloat=1.0
1.0
julia> b=unbox(Int64,theFloat)
4607182418800017408
julia> bits(theFloat)
"0011111111110000000000000000000000000000000000000000000000000000"
julia> type Words2x32
lo::Int32
hi::Int32
end
julia> c=Words2x32(0,1072693248)
Words2x32(0,1072693248)
julia> d=unbox(Float64,c)
1.0
julia> bits(d)
"0011111111110000000000000000000000000000000000000000000000000000"
julia> e=unbox(Words2x32,d)
Segmentation fault: 11
Is it a bug or I have used the function in an unexpected way?
It might help if you explain something about what you want to do with the binary representation.
struct Words32 {
int32_t lo;
int32_t hi;
};
union Both {
struct Words32 w;
int64_t i;
};
union Both b;
b.i=0x7fffffff3fffffff;
printf("lo:0x%08x\nhi:0x%08x\n",b.w.lo,b.w.hi);
hi:0x7fffffff
type Words32
hi::Int32
lo::Int32
end
a=0x7fffffff3fffffff
reinterpret(Words32,a)
gives
ERROR: reinterpret: expected bits type as first argument
in reinterpret at essentials.jl:115
For example,
julia> f(x::Float64) = reinterpret(Float64, reinterpret(UInt64, x) & 0x7fffffffffffffff)
f (generic function with 1 method)
Is this good enough?
julia> immutable Words32
hi::Int32
lo::Int32
end
julia> a = 0x7fffffff3fffffff
0x7fffffff3fffffff
julia> reinterpret(Words32, [a])
1-element Array{Words32,1}:
Words32(1073741823,2147483647)
function words(x::Float64)y = reinterpret(UInt64, x)y % UInt32, (y >> 32) % UInt32endjulia> words(1.23)(0x7ae147ae,0x3ff3ae14)julia> @code_native words(1.23).section __TEXT,__text,regular,pure_instructionsFilename: noneSource line: 2Source line: [inline] essentials.jl:119
pushq %rbpmovq %rsp, %rbp
Source line: 2Source line: [inline] essentials.jl:119movd %xmm0, %raxSource line: 3Source line: [inline] int.jl:177movq %rax, %rdxshrq $32, %rdxpopq %rbpret
Here's a simple function that gives the UInt32 words of a Float64:function words(x::Float64)y = reinterpret(UInt64, x)y % UInt32, (y >> 32) % UInt32end