I have an uint64
(what would be an unsigned long long
in C) which I want to convert in an array of bytes.
This is how I'm doing it right now (in Nim code):
Value
is typedef'd asunsigned long long
Byte
is typedef'd asunsigned char
CD
is an array ofByte
s
proc writeValue(v:Value) =
CD.add(Byte(v shr 56))
CD.add(Byte(v and Value(0x00ff000000000000)))
CD.add(Byte(v and Value(0x0000ff0000000000)))
CD.add(Byte(v and Value(0x000000ff00000000)))
CD.add(Byte(v and Value(0x00000000ff000000)))
CD.add(Byte(v and Value(0x0000000000ff0000)))
CD.add(Byte(v and Value(0x000000000000ff00)))
CD.add(Byte(v and Value(0x00000000000000ff)))
And this is how I'm reading it back (get an uint64
starting from a specific position in the array: IP
)
template readValue():Value =
inc(IP,8); (Value(CD[IP-8]) shl 56) or (Value(CD[IP-7]) shl 48) or (Value(CD[IP-6]) shl 40) or (Value(CD[IP-5]) shl 32) or (Value(CD[IP-4]) shl 24) or (Value(CD[IP-3]) shl 16) or (Value(CD[IP-2]) shl 8) or Value(CD[IP-1])
Is there a more efficient way? Am I wasting performance the way I'm doing it?