# Why does 2^100 give 0?

**URL:** <https://discourse.julialang.org/t/why-does-2-100-give-0/49607>\
**Category:** New to Julia\
**Tags:** question, type\
**Created:** [November 4, 2020, 10:10pm UTC](https://discourse.julialang.org/t/why-does-2-100-give-0/49607 "2020-11-04T22:10:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [November 4, 2020, 10:10pm UTC](https://discourse.julialang.org/t/why-does-2-100-give-0/49607/1 "2020-11-04T22:10:45Z")

</div>

As the title says, if I input a large enough computation into the REPL, it outputs 0, no matter whether I store the value or not.

I understand that as I’m on a 64-bit system, my default type for integers is `Int64`. So, when I input `2^100`, shouldn’t it either  
a) upgrade it to `Int128`  
or  
b) overflow, and then modulo to give me the value `2^100-2^63+1`?

Moreover, `BigInt(2^100)` also returns 0, but when I check the type of that 0, it indeed is `BigInt`.

My sincerest apologies for such a dumb question, but I’m kinda stuck, and I think I’m misunderstanding some key concept, hence I ask for help.

Thank You  
Warm Regards

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [November 4, 2020, 10:15pm UTC](https://discourse.julialang.org/t/why-does-2-100-give-0/49607/2 "2020-11-04T22:15:15Z")

</div>

Great question, thanks for asking.

I think this section on [integer arithmetic](https://docs.julialang.org/en/v1/manual/faq/#faq-integer-arithmetic) answers your question better than I could.

Appending Edit: Alan Edelman recently gave a [lecture on floating point arithmetic](https://youtu.be/fL8vYG69EhE). Hope it helps!

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [November 4, 2020, 10:15pm UTC](https://discourse.julialang.org/t/why-does-2-100-give-0/49607/3 "2020-11-04T22:15:55Z")

</div>

> [@Ayush\_Rawat](#):
>
> `BigInt(2^100)`

Try `BigInt(2)^100`

Otherwise you’re just doing `BigInt(0)`

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 4, 2020, 10:18pm UTC](https://discourse.julialang.org/t/why-does-2-100-give-0/49607/4 "2020-11-04T22:18:20Z")

</div>

Powers of 2 are (generally) implemented as a bitshift

```julia
julia> 2^62 |> bitstring
"0100000000000000000000000000000000000000000000000000000000000000"

julia> 2^63 |> bitstring
"1000000000000000000000000000000000000000000000000000000000000000"

julia> 2^64 |> bitstring
"0000000000000000000000000000000000000000000000000000000000000000"

```

Once you’re past 64 you’re just multiplying 0. BigInt can handle it (note that BigInt isn’t the same as Int128, it’s arbitrary size.)

```julia
julia> big(2)^64
18446744073709551616

julia> Int128(2)^64
18446744073709551616

julia> Int128(2)^128
0

julia> big(2)^128
340282366920938463463374607431768211456

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 4, 2020, 10:24pm UTC](https://discourse.julialang.org/t/why-does-2-100-give-0/49607/5 "2020-11-04T22:24:32Z")

</div>

> [@Ayush\_Rawat](#):
>
> b) overflow, and then modulo to give me the value `2^100-2^63+1` ?

It does give you a modulo and that is the zero you see. I’m not sure why you expect `2^100-2^63+1` but it’s quite clear that that number can’t possibly be represented in 64 bits as it’s larger than `2^99`.

---

<div class="post-metadata">

**Author:** ![Ayush\_Rawat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ayush_rawat/32/18961_2.png) [@Ayush\_Rawat](https://discourse.julialang.org/u/Ayush_Rawat)\
**Post date:** [November 5, 2020, 2:34pm UTC](https://discourse.julialang.org/t/why-does-2-100-give-0/49607/6 "2020-11-05T14:34:00Z")

</div>

My apologies, I forgot the constant in my head. I was expecting 2^{100}(\text{mod } 2^{63}-1) \text{ i.e. } 2^{100} -K2^{63}+K.

Which is still grossly incorrect, but my reasoning was that everytime I get to 2^{64}-1, it’ll zero out.

But of course, in reality, it’ll go to -2^{63}. Hence, our expectation should be 2^{100} \text{ mod } 2^{64}.

Thank You for the help. Sincerest apologies for such an error.
