|
From: Per W. <pw...@ia...> - 2003-03-05 12:53:30
|
And if you check closer, for shift operations it's the >> that is implementation specific, i.e. if it will do an arithmetic or logic shift right for signed numbers. The << is always ok, i.e. will always shift in a zero. Is's only when you try to use it to multiply with 2, you have to note that because of overflow, you might shift away your sign bit, resulting in a bit of confusion, i.e. [16-bit integers]: -20000 (0xB1E0) << 1 results in 25536 (0x63c0) but then all operators will give more or less strange results when operated outside the available numeric range. About compatibility with strange architectures, I don't expect us to have to see too many machines with 9-bit characters and 36-bit integers anymore, as existed for some big old iron. Such a computer manufacturer would now have too hard a time trying to attract both compiler vendors and actual end users. /Per W On Wed, 5 Mar 2003, Ioannis Vranos wrote: > -------Original Message------- > > From: Per Westermark > Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 2:04:36 =EC=EC > To: Ioannis Vranos > Cc: Dev-C++ Mailing List > Subject: Re: [Dev-C++] Character variables > > The shift operators are of course defiend for both signed and unsigned > data types. > > It's just that for >> you may not assume if it will shift in a '0' or a > '1' in the most significant bit fir signed integers, i.e. > > -3 (0xfffd) >> 1 =3D 32766 (0x7ffe) > -3 (0xfffd) >> 1 =3D -2 (0xfffe) > > depending on implementation. > > However, when shifting in characters into a 32-bit integer, I don't have > to care about this, since << always shifts in 0 in the least significant > bit. > > > =3D=3D> Yes bitwise operators can be applied to integral types signed and > unsigned however in signed types the behaviour is implementation > dependent/undefined. This is explicitly mentioned in the C99 standard: > > =3D=3D> "Some operators (the unary operator ~, and the binary operators <= <, >>, > &, ^, and |, > collectively described as bitwise operators) are required to have operand= s > that have > integer type. These operators return values that depend on the internal > representations of > integers, and have implementation-defined and undefined aspects for signe= d > types." > > > =3D=3D> The above is not mentioned explicitly in the C++98 standard, but = it is > common sense. That's why i spoke about bad magic. :) > > > > Ioannis Vranos > > * Programming pages: http://www.noicys.freeurl.com > * Alternative URL: http://run.to/noicys |