# Converting Modular to Integer type

**URL:** <https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285>\
**Category:** Help\
**Tags:** ada\
**Created:** [March 24, 2026, 11:04pm UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285 "2026-03-24T23:04:49Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZenitDS](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zenitds/32/949_2.png) [@ZenitDS](https://forum.ada-lang.io/u/ZenitDS)\
**Post date:** [March 24, 2026, 11:04pm UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/1 "2026-03-24T23:04:49Z")

</div>

Hi,

Quick newbie question. From what I have seen, I can do this to convert from I64 (signed 64 bit) to U64 (unsigned 64 bit):

```ada
function To_U64(v: I64) return U64 is
begin
    return U64'Mod(v);
end To_U64;

```

Is there a similar approach for doing the reverse, going from U64 to I64?  
I guess it might not be that simple since the mapping of 0 is undetermined for a given arbitrary range. Then what approach would you recommend?

Thanks in advance 😄

---

<div class="post-metadata">

**Author:** ![pmnw](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/p/c37758/32.png) [@pmnw](https://forum.ada-lang.io/u/pmnw)\
**Post date:** [March 25, 2026, 1:34am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/2 "2026-03-25T01:34:28Z")

</div>

Perhaps the following?

```ada
function To_I64 is new Ada.Unchecked_Conversion (Source => U64, Target => I64);

```

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [March 25, 2026, 1:55am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/3 "2026-03-25T01:55:05Z")

</div>

> [@ZenitDS](#):
>
> Then what approach would you recommend?

This depends on exactly what you’re doing; if it’s simply reinterpreting the bits, then the `Unchecked_Conversion` is the way to go. If, on the other hand, it’s something where you want things to map ‘naturally’, then something like `Long_Integer(X)` might be appropriate. If, on the third hand, you need to watch your bounds, then something like:

```ada
Function Convert(X : Interfaces.Unsigned_64) return Interfaces.Integer_64 is
(if X >= Interfaces.Unsigned_64( Interfaces.Integer_64'Last ) then Interfaces.Integer_64'Last
 elsif X <= 0 then 0
 else Interfaces.Integer_64( X )
);

```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![dmitry-kazakov](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/dmitry-kazakov/32/522_2.png) [@dmitry-kazakov](https://forum.ada-lang.io/u/dmitry-kazakov)\
**Post date:** [March 25, 2026, 8:22am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/4 "2026-03-25T08:22:54Z")

</div>

Use the standard type conversion:

```ada
   X := I64 (Y);
   Y := U64 (X);

```

It will raise Constraint\_Error when the value is out of range.

---

<div class="post-metadata">

**Author:** ![ZenitDS](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zenitds/32/949_2.png) [@ZenitDS](https://forum.ada-lang.io/u/ZenitDS)\
**Post date:** [March 25, 2026, 8:39am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/5 "2026-03-25T08:39:10Z")

</div>

Hi, thanks everyone for the answers. What I am trying to do is to do the inverse function, that is, to map it as if it was a two’s complement conversion. I am not sure if Unchecked\_Conversion is what I want, does this conversion depend on the internal representation of the machine or is it consistent across platforms? e.g. endianness

---

<div class="post-metadata">

**Author:** ![dmitry-kazakov](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/dmitry-kazakov/32/522_2.png) [@dmitry-kazakov](https://forum.ada-lang.io/u/dmitry-kazakov)\
**Post date:** [March 25, 2026, 8:50am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/6 "2026-03-25T08:50:25Z")

</div>

Do you want to map negative signed numbers -I to 2\*\*N - I? Then you write just that:

```ada
   if I < 0 then
      return U64'Last - U64 (-1 - I);
   else
      return U64 (I);
   end if;

```

---

<div class="post-metadata">

**Author:** ![ZenitDS](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zenitds/32/949_2.png) [@ZenitDS](https://forum.ada-lang.io/u/ZenitDS)\
**Post date:** [March 25, 2026, 9:48am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/7 "2026-03-25T09:48:43Z")

</div>

Well, I believe that the code above does that, the problem comes when going the other way around. Coming from an unsigned and then going to the signed version.

I have arrived at this:

```ada
    function To_I64(v: U64) return I64 is
    begin
        return (if v <= U64(I64'Last) then I64(v)
                else -I64(U64'Last - v + 1));
    end To_I64;

```

Just wondering if there is any other way of doing this.

---

<div class="post-metadata">

**Author:** ![dmitry-kazakov](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/dmitry-kazakov/32/522_2.png) [@dmitry-kazakov](https://forum.ada-lang.io/u/dmitry-kazakov)\
**Post date:** [March 25, 2026, 10:18am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/8 "2026-03-25T10:18:15Z")

</div>

> [@ZenitDS](#):
>
> `To_I64(v: U64) return I64 is`

Note that 2’s complement has asymmetric range -2 \*\* N .. 2 \*\* N - 1. The edge case is that there is no -I64’First. So your code is incorrect. You must subtract 1 **after** the type conversion.

BTW, you can use bit-wise operations instead of subtraction, e.g..

```ada
      Last : constant := U64'Modulus / 2;
   begin
      if X >= Last then
         return -I64 (not X) - 1;
      else
         return I64 (X);
      end if;

```

---

<div class="post-metadata">

**Author:** ![ZenitDS](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zenitds/32/949_2.png) [@ZenitDS](https://forum.ada-lang.io/u/ZenitDS)\
**Post date:** [March 25, 2026, 10:36am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/9 "2026-03-25T10:36:46Z")

</div>

You are right, changed it. One last question, is the representation of two’s complement uniform across machines in Ada?

---

<div class="post-metadata">

**Author:** ![dmitry-kazakov](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/dmitry-kazakov/32/522_2.png) [@dmitry-kazakov](https://forum.ada-lang.io/u/dmitry-kazakov)\
**Post date:** [March 25, 2026, 10:43am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/10 "2026-03-25T10:43:40Z")

</div>

I believe all modern processors are 2’s complement.

---

<div class="post-metadata">

**Author:** ![Irvise](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/i/7ba0ec/32.png) [@Irvise](https://forum.ada-lang.io/u/Irvise)\
**Post date:** [March 25, 2026, 10:56am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/11 "2026-03-25T10:56:40Z")

</div>

AFAIK, in the past maybe some machines were not two’s complement, but nowadays even modern C++ does not support non-two’s complement machines [P0907R4: Signed Integers are Two’s Complement](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r4.html)

In Ada probably the compiler has to generate correct code, as in the mathematically correct code, in a deterministic manner. So the compiler will generate whatever is needed to fulfill the operation.

---

<div class="post-metadata">

**Author:** ![ZenitDS](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zenitds/32/949_2.png) [@ZenitDS](https://forum.ada-lang.io/u/ZenitDS)\
**Post date:** [March 25, 2026, 11:07am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/12 "2026-03-25T11:07:57Z")

</div>

Thanks! I think I will keep the subtracting version, rather than the complement one. My inner C programmer has an uncanny feeling when depending on bit representation. 🙂

---

<div class="post-metadata">

**Author:** ![Irvise](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/i/7ba0ec/32.png) [@Irvise](https://forum.ada-lang.io/u/Irvise)\
**Post date:** [March 25, 2026, 11:14am UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/13 "2026-03-25T11:14:56Z")

</div>

> [@ZenitDS](#):
>
> My inner C programmer has an uncanny feeling when depending on bit representation. 🙂

Oh… I was there… Oh… You are about to discover the most amazing language when it comes to bit and low-level aspects… You are going to love Ada! Here is an interesting book/chapter that may help you a lot [Ada for the Embedded C Developer - learn.adacore.com](https://learn.adacore.com/courses/Ada_For_The_Embedded_C_Developer/index.html) but most importantly [Types and Representation - learn.adacore.com](https://learn.adacore.com/courses/advanced-ada/parts/data_types/types_representation.html)

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [March 25, 2026, 5:11pm UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/14 "2026-03-25T17:11:15Z")

</div>

> [@ZenitDS](#):
>
> One last question, is the representation of two’s complement uniform across machines in Ada?

This question is… confusing.  
Ada doesn’t _require_ two’s complement, and that’s a property of the processor anyway. (Randy’s Janus has an implementation that runs/ran on a one’s complement machine, IIRC.)

It’s essentially a moot point, because all modern machines are two’s complement, and likely to be that way unless and until someone catapults some alternate architecture (probably _radically_ different, like balanced trinary) into [semi-]mainstream usage.

(Experimental hardware, FPGAs and the like excepted, of course.)

> [@Irvise](#):
>
> Oh… I was there… Oh… You are about to discover the most amazing language when it comes to bit and low-level aspects…

When you **really** need the control, you can’t beat record-representation clauses and manually laying things out. (I did this for a 4- and 8-bit floating point, that was… an interesting exercise.)

---

<div class="post-metadata">

**Author:** ![SweetAda](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/s/7993a0/32.png) [@SweetAda](https://forum.ada-lang.io/u/SweetAda)\
**Post date:** [March 25, 2026, 9:45pm UTC](https://forum.ada-lang.io/t/converting-modular-to-integer-type/4285/15 "2026-03-25T21:45:38Z")

</div>

Every time you look inside a value or you play with any bit-oriented operation, you are implicitly assuming a representation, which is in this case 2’s complement (nearly all CPUs of the known universe). There is no way to correctly adjust bits if you do not known the internal representation.

Playing with bits in arithmetic context with and, or, xor, whatever, is formally equivalent to a bunch of UC from that point of view (because you bypass the language all way down to the hardware), and code would be just not portable to a non-2’s-complement machine.

Sorry for the trivia.
