comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Interfaces.Shift_Left
Date: Tue, 13 Dec 2011 15:48:47 -0600
Date: 2011-12-13T15:48:47-06:00	[thread overview]
Message-ID: <jc8h83$86p$1@munin.nbi.dk> (raw)
In-Reply-To: 5f989095-5c1c-4b23-a538-d70f4718b4b1@l19g2000yqc.googlegroups.com

"awdorrin" <awdorrin@gmail.com> wrote in message 
news:5f989095-5c1c-4b23-a538-d70f4718b4b1@l19g2000yqc.googlegroups.com...
> The data being manipulated does start out as signed 32-bit integers -
> its the Logical Shift operations in the Interfaces package that are
> expecting Unsigned_32 types as input parameters.

It does not make sense to "shift" numbers: "shift" is not an operation of a 
number.

It does make sense to "shift" a bunch of bits. Ada allows treating an 
unsigned number as a "bunch of bits", thus modular types support "and", 
"or", and shifting. But Ada does not allow that for signed numbers.

C programmers sometimes use shifting as a shortcut for multiplying or 
dividing by a power of 2. But this is premature optimization: any remotely 
decent compiler will do this automatically, without confusing the reader of 
the code. (Ever Ada compiler that I've examined the output of does this 
optimization.) (And this has the advantage of still getting overflow checks 
for multiplies, so there is a protection against getting the wrong answers 
by accident.)

So, if the data is *really* numeric in nature, then the first choice is to 
write the operations in terms of multiply and divide.

OTOH, if the data is not numeric, then the shifting probably is just used to 
pack data into a word. That's better done in Ada using representation 
clauses -- but in the interests of getting your program converted fast, you 
probably should leave well-enough alone and stick with the shifts. But in 
that case, the data type should be unsigned (there is no "number" involved).

Anyway, I understand if you want to use Unchecked_Conversion as a 
quick-and-dirty hack -- those are often necessary in the real world. But you 
shouldn't think of that as the final solution; there is something 
fundementally wrong with needing to shift signed values, and it would be 
best to work out what the problem is an eliminate it.

                                   Randy.





  reply	other threads:[~2011-12-13 21:48 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-12 22:30 Interfaces.Shift_Left awdorrin
2011-12-12 23:34 ` Interfaces.Shift_Left Simon Wright
2011-12-13  1:36   ` Interfaces.Shift_Left Adam Beneschan
2011-12-13 12:00 ` Interfaces.Shift_Left Brian Drummond
2011-12-13 13:15   ` Interfaces.Shift_Left awdorrin
2011-12-13 21:48     ` Randy Brukardt [this message]
2011-12-14 18:28       ` Interfaces.Shift_Left awdorrin
2011-12-14 22:49         ` Interfaces.Shift_Left Randy Brukardt
2011-12-15  9:51           ` Interfaces.Shift_Left Niklas Holsti
2011-12-16  0:23             ` Interfaces.Shift_Left Randy Brukardt
2011-12-18 20:47               ` Interfaces.Shift_Left Niklas Holsti
2011-12-20  0:38                 ` Interfaces.Shift_Left Randy Brukardt
2011-12-20  2:18                   ` Interfaces.Shift_Left Shark8
2011-12-20 10:08                   ` Interfaces.Shift_Left Dmitry A. Kazakov
2011-12-20 19:38                   ` Interfaces.Shift_Left Niklas Holsti
2011-12-20 20:46                     ` Interfaces.Shift_Left Niklas Holsti
2011-12-20 21:13                       ` Interfaces.Shift_Left Simon Wright
2011-12-20 21:08                     ` Interfaces.Shift_Left Simon Wright
2011-12-20 23:26                       ` Interfaces.Shift_Left Randy Brukardt
2011-12-20 23:36                     ` Interfaces.Shift_Left Randy Brukardt
2011-12-21  0:44                       ` Interfaces.Shift_Left Georg Bauhaus
2011-12-21  7:23                       ` Interfaces.Shift_Left AdaMagica
replies disabled

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