Hello,
I started using Julia yesterday. My objective to rewrite a data-analysis program written in Python that currently takes more than 24 hours, and cut it down to a few hours.
The first hurdle is the following. From a bitstring (>300bit) I isolate 8-bit in an Int64 variable. The problem is that my 8-bit represents a signed integer. How do I get from the 8-bits in Int64 to Int8 (-128, 127) elegantly and fast? Right now the result ranges (0, 255).
My first question would be - how are you reading/parsing that bitstring right now? In what form are you reading it in? Intuitively, I’d skip the step with reading it in as a Int64 entirely and directly read a Int8 instead.
I don’t think unsigned does anything here, & treats the data as raw bits anyway. And -20 is already an Int64, as are the data the OP has. So, can just as well write:
julia> UInt8(-20 & 0xff)
0xec
Or, for clarity,
julia> UInt8(-20 & typemax(UInt8))
0xec
(Edit: BTW, shouldn’t it be turned into an Int8, not an UInt8? I think @cjdoris’s approach is the right one.)
But I agree with @Sukera, it seems suboptimal put the data into an Int64 in the first place.
Thanks, I see now that unsigned was unnecessary, and I solved the wrong problem because I didn’t read the OP’s problem carefully enough. I did include the redundant Int64 intentionally to show that we were operating on that type because that’s how the OP worded the problem.