# Converting Int64 to Int8

**URL:** <https://discourse.julialang.org/t/converting-int64-to-int8/71967>\
**Category:** New to Julia\
**Created:** [November 23, 2021, 7:30pm UTC](https://discourse.julialang.org/t/converting-int64-to-int8/71967 "2021-11-23T19:30:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![frits45](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@frits45](https://discourse.julialang.org/u/frits45)\
**Post date:** [November 23, 2021, 7:30pm UTC](https://discourse.julialang.org/t/converting-int64-to-int8/71967/1 "2021-11-23T19:30:31Z")

</div>

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).

Thanks for any suggestion

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [November 23, 2021, 7:40pm UTC](https://discourse.julialang.org/t/converting-int64-to-int8/71967/2 "2021-11-23T19:40:17Z")

</div>

A M(inimal) W(orking) E(xample) is always a good start.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [November 23, 2021, 7:53pm UTC](https://discourse.julialang.org/t/converting-int64-to-int8/71967/3 "2021-11-23T19:53:54Z")

</div>

```julia
julia> UInt8(unsigned(Int64(-20))& 0xff)
0xec

```

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [November 23, 2021, 10:53pm UTC](https://discourse.julialang.org/t/converting-int64-to-int8/71967/4 "2021-11-23T22:53:00Z")

</div>

I think `mod(x, Int8)` does what you want.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 24, 2021, 12:56am UTC](https://discourse.julialang.org/t/converting-int64-to-int8/71967/5 "2021-11-24T00:56:32Z")

</div>

> [@frits45](#):
>
> 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?

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.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 24, 2021, 7:45am UTC](https://discourse.julialang.org/t/converting-int64-to-int8/71967/6 "2021-11-24T07:45:03Z")

</div>

> [@Jeff\_Emanuel](#):
>
> ```julia
> julia> UInt8(unsigned(Int64(-20))& 0xff)
> 0xec
> 
> ```

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
julia> UInt8(-20 & 0xff) 
0xec

```

Or, for clarity,

```julia
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.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 24, 2021, 9:06am UTC](https://discourse.julialang.org/t/converting-int64-to-int8/71967/7 "2021-11-24T09:06:17Z")

</div>

Another option:

```julia
julia> -20 % Int8
-20

julia> typeof(ans)
Int8

julia> -20 % UInt8
0xec

julia> typeof(ans)
UInt8

```

Edit: Didn’t see this was suggested above already.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 24, 2021, 12:59pm UTC](https://discourse.julialang.org/t/converting-int64-to-int8/71967/8 "2021-11-24T12:59:03Z")

</div>

I agree a minimal working example would help to understand what is really needed… From what I understand you might be looking for

```julia
# A Int64 value corresponding to the bits `11111111`
a = 255

# The same 8 bits intrepreted as Int8
signed(UInt8(a))

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [November 24, 2021, 3:02pm UTC](https://discourse.julialang.org/t/converting-int64-to-int8/71967/9 "2021-11-24T15:02:31Z")

</div>

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.
