# Why is 2^63 negative?

**URL:** https://discourse.julialang.org/t/why-is-2-63-negative/48513
**Category:** New to Julia
**Tags:** integer-overflow
**Created:** [October 16, 2020, 7:18pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513 "2020-10-16T19:18:34Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![timvink](https://avatars.discourse-cdn.com/v4/letter/t/e8c25b/32.png) [@timvink](https://discourse.julialang.org/u/timvink)
#### Post date: [October 16, 2020, 7:18pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/1 "2020-10-16T19:18:34Z")

</div>

Hi 👋

Making my first steps into julia, coming from R and python. Following the getting started guide, I noticed the following:

```julia
2^62
# 4611686018427387904
2^63 # <--- This is negative?
# -9223372036854775808
2^63 > 1
# false
2^64
# 0 # this is zero, but that makes sense if it is a 64 bit float

```

I’d be interested to understand why?

For reference, python and R below:

```python
2^63 > 1
# true

```

```nohighlight
2^63 > 1
# [1] TRUE

```

---

<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: [October 16, 2020, 7:22pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/2 "2020-10-16T19:22:54Z")

</div>

In Julia, `2` is an integer literal with 64 bits length. `2^63` overflows into the most signifant bit and thus is negative:

```julia
julia> bitstring(2)
"0000000000000000000000000000000000000000000000000000000000000010"

julia> bitstring(2^32)
"0000000000000000000000000000000100000000000000000000000000000000"

julia> bitstring(2^63)
"1000000000000000000000000000000000000000000000000000000000000000"

```

Since julia is using machine integers (which use Twos-Complement), an integer with a leading 1 is negative.

In contrast, python automatically grows the integer type as necessary - this is much slower for general use though, which is why its not done in julia.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [October 16, 2020, 7:35pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/3 "2020-10-16T19:35:31Z")

</div>

Is it possible to have an error when this happens, eg using a “safe” version of Int64?  
Would this check cost “a lot”?

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [October 16, 2020, 7:38pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/4 "2020-10-16T19:38:27Z")

</div>

> [@sylvaticus](#):
>
> using a “safe” version of Int64

Yep, that’s `SafeInt64` from [SaferIntegers.jl](https://github.com/JeffreySarnoff/SaferIntegers.jl)

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [October 16, 2020, 7:38pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/5 "2020-10-16T19:38:28Z")

</div>

There is [SaferIntegers.jl](https://github.com/JeffreySarnoff/SaferIntegers.jl), if you want checked arithmetic with integers. You can also use functions in `Base.Checked` directly, like `Base.Checked.checked_add`, for example.

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [October 16, 2020, 8:13pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/6 "2020-10-16T20:13:20Z")

</div>

In R, a few factors differ from Julia:

- `2` isn’t an integer – it’s a floating point number. You need `2L` to get an integer.
- `x^y` always produces a floating point number, even for simple cases.

```julia
> x <- 2L
> y <- 4L
> typeof(x)
[1] "integer"
> typeof(y)
[1] "integer"
> typeof(x^y)
[1] "double"

```

There are similar pitfalls to R’s approach that are just subtler than Julia’s pitfalls:

```julia
> 2^58 + 1 == 2^58
[1] TRUE

```

```julia
julia> 2^58 + 1 == 2^58
false

```

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [October 16, 2020, 8:34pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/7 "2020-10-16T20:34:18Z")

</div>

Just for completeness: in NumPy, the integer type (np.int64) behaves the same as in Julia.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [October 16, 2020, 8:48pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/8 "2020-10-16T20:48:35Z")

</div>

And we do this for the same reason: this is the behavior of our machines’ integers. It’s the fastest behavior possible. Doing anything else requires extra operations to emulate some different sort of behavior.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [October 16, 2020, 8:52pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/9 "2020-10-16T20:52:52Z")

</div>

For the how much does this cost question, the answer is anywhere from 10% to 20x. A typical cost is usually in the 2x case, but the added complication can easily prevent massive optimizations.

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [October 16, 2020, 9:42pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/10 "2020-10-16T21:42:09Z")

</div>

Should we add a link to [Learn Julia in Y Minutes](https://learnxinyminutes.com/docs/julia/) in the [Please read: make it easier to help you - #23](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/23) since there is an explicit example about integer overflow within the first 10 lines, this seems to be a recurrent stumbling point for ex-python users? (cc @Tamas_Papp)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 17, 2020, 7:43am UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/11 "2020-10-17T07:43:34Z")

</div>

I don’t think that PSA should be a link to various docs other than the manual, or target users from specific programming languages. Once you start doing that, it gets too long (it already is) and even fewer people would read it.

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [October 17, 2020, 8:01am UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/12 "2020-10-17T08:01:12Z")

</div>

Fair, indeed there is [Frequently Asked Questions · The Julia Language](https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic) it’s a bit lengthy for newcomers though…

Maybe there should be another PSA gathering links to frequently asked questions that we could at least point to when the questions resurface

---

<div class="post-metadata">

### Author: ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)
#### Post date: [October 17, 2020, 12:47pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/13 "2020-10-17T12:47:33Z")

</div>

Sometimes I wonder if it would be a good idea to make a blog post about stuff like this. Hit some of the classics like 1.0 != 1.0 (in julia this is actually handled very well compared to say C/C++). By showing some of the boundaries of computing issues we get the opportunity to teach some computer science stuffs to people while introducing the language. Could be fun to show off the interop of Julia too, but maybe this is getting off topic.

Edit someone asked me what I mean by 1.0 != 1.0. Unfortunately Cxx isn’t working with Julia 1.4+ but you can get this kind of behaviour with big floats :). So for example:

```julia
a = BigFloat(0.1)
BigFloat(1.0) == BigFloat(10.) * a
# therefore 1.0 != 1.0

```

So in C/C++ you will deal with this issue a bit, its due to numerical precision. Meanwhile - Julia is great because

```nohighlight
1.0 == 1.0
#woo hoo

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 17, 2020, 2:17pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/14 "2020-10-17T14:17:44Z")

</div>

> [@tlienart](#):
>
> it’s a bit lengthy for newcomers though…

It is impossible to please everyone — if parts of that FAQ (one of the oldest, actually) were omitted, I imagine people would suggest that they are added. At least when they are there, the reader has the option of just skimming through sections.

---

<div class="post-metadata">

### Author: ![waldyrious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waldyrious/32/80_2.png) [@waldyrious](https://discourse.julialang.org/u/waldyrious)
#### Post date: [October 18, 2020, 9:09am UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/15 "2020-10-18T09:09:37Z")

</div>

> [@tlienart](#):
>
> Fair, indeed there is [https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic](https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic) it’s a bit lengthy for newcomers though…

IMHO the short comments in this thread from [@ericphanson](https://discourse.julialang.org/t/why-is-2-63-negative/48513/4), [@simeonschaub](https://discourse.julialang.org/t/why-is-2-63-negative/48513/5), [@mbauman](https://discourse.julialang.org/t/why-is-2-63-negative/48513/8) and [@Oscar\_Smith](https://discourse.julialang.org/t/why-is-2-63-negative/48513/9) concisely cover most of the information that newcomers typically want when first encountering this issue. I agree with @Tamas_Papp that we shouldn’t remove information from the FAQ entry, but maybe it could be reorganized in a way that it starts off with similar concise and to-the-point descriptions, and then goes into more technical detail explaining the background and motivation for these decisions.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 18, 2020, 12:02pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/16 "2020-10-18T12:02:25Z")

</div>

I think that there are three layers of answers to the original question in this post:

1. _Why is `2^63` negative?_ Because Julia uses overflow/wraparound/native arithmetic.

2. _Why does Julia do this?_ Because it is fast.

3. _What if I don’t like this?_ That’s fine, Julia is super-extensible. Just use one of the relevant packages (list follows: …) if they implement what you want, or roll your own.

Separating the answer to subheadings along these lines would be nice. (My wording above is informal, a PR should consider rephrasing).

---

<div class="post-metadata">

### Author: ![timvink](https://avatars.discourse-cdn.com/v4/letter/t/e8c25b/32.png) [@timvink](https://discourse.julialang.org/u/timvink)
#### Post date: [October 19, 2020, 7:10pm UTC](https://discourse.julialang.org/t/why-is-2-63-negative/48513/17 "2020-10-19T19:10:57Z")

</div>

Thanks everyone for the helpful answers, I learned quite a lot from this!

> Making my first steps into julia

This was also my first impression of the Julia community, which is really good! 😄
