octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bitshift of int8, int16, etc?


From: David Bateman
Subject: Re: bitshift of int8, int16, etc?
Date: Mon, 25 Jun 2007 10:00:11 +0200
User-agent: Thunderbird 1.5.0.7 (X11/20060921)

Daniel J Sebald wrote:
>
> Does the proposed version limit the class value to the maximum
> inherently?  E.g., say it is int8 type and bit shift is +1 and current
> value is 127; Is ival = 127 << 1 equal to 254 but then treated as 126
> in all uses of the int8 value?
ival is of type T where T is the type used to instantiate the class.
Therefore for octave_int8, T is of type int8_t. Try the C++ code sample

#include <iostream>
#include <inttypes.h>

int main (int argc, char **argv)
{
  int8_t a = atoi(argv[1]);
  int shift = atoi(argv[2]);
  int8_t b;
  if (shift > 0)
    b = (a << shift);
  else
    b = (a >> -shift);
  std::cerr << static_cast<double>(b) << std::endl;
}

and compiling to "temp", I get the following

% ./temp 63 1
126
%. /temp 64 1
-128
% ./temp 127 1
-2
% ./temp 127 -1
63
% ./temp -1 -1
-1

which to me seems to be the right behavior for an arithmetic shift
operator..

>
> The thing I wonder is that if as you describe this, it is arithmetic
> shift and negative numbers are shifted similarly to postive numbers,
> should there also be a test on the value being no less than the
> minimum allowed value of the class?
>
>   template <class T2>
>   octave_int<T>& operator <<= (const T2& x)
>   {
>     ival = ival << x;
>     if (ival > std::numeric_limits<T>::max ())
>     ival &= std::numeric_limits<T>::max ();
>     else if (ival < std::numeric_limits<T>::min ())
>     ival |= std::numeric_limits<T>::min ();
>     return *this;
>   }
>
> For example, say int8, the value is maximum and shift is +1:
>
> <24zeros>01111111 shifted +1 becomes <24zeros>11111110 but that is
> greater than max in ival's 32 or 64 bit world so the value is and'd
> with <24zeros>01111111 which results in <24zeros>01111110.
The type of ival is int8_t and so its in the native format of the type
and not a 32 bit type as the above assumes...

>
> Or another example, say int 8, the value is minimum + 1 and shift is +1:
>
> <24ones>10000001 shifted +1 becomes <24ones>00000010 but that is less
> than min in ival's 32 or 64 bit world so the value is or'd with
> <24ones>10000000 which results in <24ones>10000010.
This is equivalent in the program above to

./temp -127 1

which gives "2" as expected... The <24ones> don't exist as again the
octave type is instantiated with int8_t..

Cheers
David


>
> (If the shift is negative, then there is nothing to worry about,
> right?)  It all depends how behavior is defined.  I'm not 100% sure
> what it should be defined as.
>


-- 
David Bateman                                address@hidden
Motorola Labs - Paris                        +33 1 69 35 48 04 (Ph) 
Parc Les Algorithmes, Commune de St Aubin    +33 6 72 01 06 33 (Mob) 
91193 Gif-Sur-Yvette FRANCE                  +33 1 69 35 77 01 (Fax) 

The information contained in this communication has been classified as: 

[x] General Business Information 
[ ] Motorola Internal Use Only 
[ ] Motorola Confidential Proprietary



reply via email to

[Prev in Thread] Current Thread [Next in Thread]