Negative Enum_Rep

Hello all,

Question about negative representation of enumeration types.

Consider the code below:

with ada.Text_IO; use ada.Text_IO;
procedure test_enum is
type enum1 is (a,b,c,d);
for enum1 use (-16,-8,0,8);
begin
for i in enum1 loop
put_line (i’img & " " & enum1’Enum_Rep(i)'img);
end loop;
end test_enum;

The output of that is as following:

A 240
B 248
C 0
D 8

Instead of the negative Enum_Rep’s I’m getting those +2**8. That was bad enough, but I was curious to see further. The following code modification:

for enum1 use ( -512,-256,0,8);

gives

A 65024
B 65280
C 0
D 8

that is 2*16+the right answer. At this point I got disoriented and decided to ask - what’s going on?
To me it looks like a bug (especially that I had no warnings from the compiler).

My system is GNAT Community 2021 under Win 11.

I just tried your example using gnat 15.2.0 and I get

$ gnat --version
GNAT 15.2.0
Copyright (C) 1996-2025, Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gnatmake forum.adb 
gcc -c forum.adb
gnatbind -x forum.ali
gnatlink forum.ali
$ ./forum 
A -512
B -256
C  0
D  8

(and the expected value for your first example)

what is the exact version that you use? (gnat -–version)

Hi.

  1. Your post is very unreadable, you should format it next time out of respect for other users.
  2. Better use standard Ada ('Img is GNAT extension).
  3. I recommend using Compiler Explorer to test different compilers, like here.

Please note the code formatting and the use of Ada 2012 Image attribute on the value without the need to specify subtypes.

EDIT:
After quick check:

A  240
B  248
C  0
D  8

only happens for x86-64 gnat 11.1. The result what it should be for the earlier and later versions I sampled.
Seems like you are using the one version that has this borked.

1 Like

By this he means, use triple backticks – or – the “code” symbol in the comment editor. (The “code” symbol looks like </>.)

Ok, thank you all, everything is clear now.

If it wasn’t clear, you only need to enclose code in backticks:

procedure Hello is
begin
   Ada.Text_IO.Put_Line ("Hello, world!");
end Hello;

If it was clear, then never mind – I was just wondering because I saw that your reply was entirely in backticks.