Re: [Dev-C++] Shift operators portability
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-09-23 06:07:04
|
They don't wrap around since they are shift operators and not rotate
operators.
Some care should be used with negative numbers since most machines are
using two-complement numbers which high bit specifies if number is
negative. By shifting that bit, you definitely don't get mult/div with
two.
As long as you don't try to load only a part of a number and perform a
shift on, big or little endian doesn't matter. The endianness is only how
the number is stored in memory. When loaded into the processor, it is just
a number.
Big or little endian doesn't matter - unless a number is processed
The sample program below doesn't work becase it isn't assigning the result
of the shifts.
count <<= 1;
count >>= 1;
or
count = count << 1;
count = count >> 1;
/Per W
On Mon, 22 Sep 2003, Michael D. Smith wrote:
>
> No wraparound, they drop off the end, at least on any machine I've ever
> been familiar with.
>
> I thought I would try, see how it works on this machine and got nothing. No
> compiler error, and no change in the int.
>
> Anyone know why this produces nothing (16) every time? And I tried other
> numbers, in both positions, as well. Even tried turning it around -- Same
> thing, nothing, every time. It seems there should be some effect, or an
> error..
>
> I have an AMD processor and no idea if it's big or little endian.
>
>
>
> #include<iostream.h>
> #include<stdlib.h>
> #include<fstream.h>
>
> int main()
> {
> int count = 16;
>
> count >> 2;
> cout << "count = " << count << "\n";
>
> 2 << count;
> cout << "count = " << count << "\n";
>
> system ("pause");
> return 0;
> }
>
>
> At 08:14 PM 9/22/03, you wrote:
> >Hi there.
> >
> >Are the shift operators ( << and >> ) portable?
> >i.e.
> >Does x << 1 have the same effect as multiplying by 2 ?
> >and
> >Does x >> 1 have the same effect as dividing by 2?
> >On both big and little endian order machines?
> >
> >also is there any wraparound?
> >i.e. in a shift the bits that are pushed out of the data structure placed
> >at the back-end of the data structure (on the other side of where they
> >were pushed out)?
> >
> >Thanks in Advance
> >David McKen
> >
> >
> >Do you Yahoo!?
> ><http://us.rd.yahoo.com/evt=10469/*http://sitebuilder.yahoo.com>Yahoo!
> >SiteBuilder - Free, easy-to-use web site design software
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|