comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.dash-bauhaus@futureapps.de>
Subject: Re: condition true or false? ->  (-1 < sizeof("test"))
Date: Tue, 22 May 2012 12:29:59 +0200
Date: 2012-05-22T12:29:58+02:00	[thread overview]
Message-ID: <4fbb6aa6$0$9522$9b4e6d93@newsspool1.arcor-online.net> (raw)
In-Reply-To: <jper36$8t8$1@speranza.aioe.org>

On 22.05.12 03:45, glen herrmannsfeldt wrote:

> I don't know how ADA treats signed values, or how its bitwise
> operators work. This is still posted to the ADA group, though.

A feature of Ada (a lady's first name; some older book's
have capitals in titles) is that you would not normally have
to worry; you still can if you like, or must.

First, one cannot store signed (unsigned) values in places whose
type is unsigned (signed) unless using force such as explicit
type conversion, or Unchecked_Conversion. Different types,
no implicit conversions, as mentioned in the thread.

As needed, one selects from the three kinds of types that
have logical operations, Boolean, modular, and packed array
of Boolean.

Some redundant assertions in the following.

with Interfaces;   -- for unsigned modular "hardware" types
with Ada.Unchecked_Conversion;
procedure Bops is

    -- modular types with modular arithmetic and Boolean ops:

    type U8 is mod 2**8;

    pragma Assert (U8'Pred (0) = -1);
    pragma Assert (U8'Succ (U8'Last) = 0);

    X8 : U8 := 2#1111_1111#;
    --X9 : U8 := 2#1111_1111_1#;  -- compiler rejects, value not in range
    X1 : U8 := -1;                       --  modular arithmetic
    X0 : U8 := not 0;                    --  has Boolean ops

    pragma Assert (X0 = X1);
    pragma Assert (X1 = 2#1111_1111#);

    -- convert from signed:

    type S8 is range -128 .. 127;
    for S8'Size use 8;

    function To_U8 is new Ada.Unchecked_Conversion (S8, U8);
    I8 : S8 := -1;          -- a negative signed value
    --XI : U8 := U8 (I8);   -- type conv will raise! value out of range
    XU : U8 := To_U8 (I8);   -- o.K., not checked

    pragma Assert (XU = 8#377#);

    -- unsinged_N "hardware types" when supported by the compiler;
    -- includes shifting operations and such, is modular

    use type Interfaces.Unsigned_64;     -- import "+" for literals
    U64 : Interfaces.Unsigned_64 := 16#FFFF_FFFF_FFFF_0000# + 2#101#;


    -- types for convenient access to individual bits

    type Bitvec is array (Natural range <>) of Boolean;
    pragma Pack (Bitvec);                --  guaranteed
    subtype Bitvec_8 is Bitvec (0 .. 7);

    Y : Bitvec (0 .. 11) := (5 => True, others => False);

    pragma Assert (Y(5) and not Y(11));

    Z : Bitvec_8;
    Toggle : constant Bitvec_8 := (others => True);
begin
    Y (11) := True;
    Z := Y(8 - 6 + 1 .. 8) & Y(10 .. 11) xor Toggle;
    pragma Assert (Z = (True, True, False, True,
                        True, True, True, False));
end Bops;


-- Georg



  parent reply	other threads:[~2012-05-22 10:29 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <95634f38f6ee0d116da523fdc2c9f5ca@dizum.com>
2012-05-21 15:28 ` condition true or false? -> (-1 < sizeof("test")) Nomen Nescio
2012-05-21 15:37   ` Richard Maine
2012-05-21 15:45   ` Adam Beneschan
2012-05-21 17:16     ` Robert A Duff
2012-05-22  1:08   ` Terence
2012-05-22  7:54     ` Les Neilson
2012-05-22 23:19       ` Terence
2012-05-23  7:20         ` Les Neilson
2012-05-22 15:43     ` Fritz Wuehler
2012-05-22  1:45   ` glen herrmannsfeldt
2012-05-22  6:11     ` Nomen Nescio
2012-05-22  8:00       ` Martin
2012-05-22  9:55       ` BartC
2012-05-22 12:07         ` Dmitry A. Kazakov
2012-05-22 16:25           ` BartC
2012-05-22 17:03             ` Dmitry A. Kazakov
2012-05-22 17:26               ` BartC
2012-05-22 18:18                 ` Dmitry A. Kazakov
2012-05-23  7:26                   ` Terence
2012-05-22 17:56         ` Fritz Wuehler
2012-05-22 10:29     ` Georg Bauhaus [this message]
2012-05-30  3:22   ` robin.vowels
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox