Thread: Re: [Dev-C++] Shift operators portability
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Michael D. S. <for...@hi...> - 2003-09-23 07:54:40
|
OK, with that "slight*" correction. It does multiply and divide by 4 (using
a 2), and as long as it's small numbers, and not negative numbers, it
appears to work reliably both ways and back and forth.
30 or 40 years ago, when computers were a lot slower and had almost no
memory, using this might have made sense. With a gigahertz computer, you're
probably much better off to just stick with the regular math functions. If
they are any slower, it's not by any noticeable amount, they are definitely
more portable, and much less error prone -- programmer, maintainer, and
otherwise (the dropped bits).
I'm not real sure that when you engage the math processor to multiply or
divide by 2,4,8,16 ect, that it doesn't just do this anyway -- only in a
more portable/reliable fashion.
Bottom line what's the point???
It is fun to play with though. Think about being able to control one little
bit in hundreds of millions of bytes -- now all I gotta do is figure out
which one, and what I'm gonna do with the rest of 'em :)
ms
* just the difference between working and not working
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 = count >> 2;
cout << "count = " << count << "\n";
count = count << 2;
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
|
|
From: stefan k. <ste...@ya...> - 2003-09-23 08:19:51
|
Something to keep in mind about this matter:
You probably do not have to explicitly use the >> or << operators, since
most modern compilers will optimize the code to use shifts where
possible anyway.
Of course that only works, if the divisor or multiplicator is a constant
and the division / multiplication can be performed by using a single
shift instruction. (i.e. 2/4/8/16/...)
So instead of writing:
const long kFactor = 2;
value = foo << kFactor;
You can as well simply write:
const long kFactor = 4;
value = foo * kFactor;
Which will result in a left-shift by 2, effectively. (With most C/C++
compilers today.)
The *big advantage* of this is, that you can later easily change
'kFactor' to three 3 (if that should become necessary), without having
to find and change all your shifts back to multiplications in your code!
Cheers,
Stefan
Michael D. Smith wrote:
>
> OK, with that "slight*" correction. It does multiply and divide by 4
> (using a 2), and as long as it's small numbers, and not negative
> numbers, it appears to work reliably both ways and back and forth.
>
> 30 or 40 years ago, when computers were a lot slower and had almost no
> memory, using this might have made sense. With a gigahertz computer,
> you're probably much better off to just stick with the regular math
> functions. If they are any slower, it's not by any noticeable amount,
> they are definitely more portable, and much less error prone --
> programmer, maintainer, and otherwise (the dropped bits).
>
> I'm not real sure that when you engage the math processor to multiply or
> divide by 2,4,8,16 ect, that it doesn't just do this anyway -- only in a
> more portable/reliable fashion.
>
> Bottom line what's the point???
>
> It is fun to play with though. Think about being able to control one
> little bit in hundreds of millions of bytes -- now all I gotta do is
> figure out which one, and what I'm gonna do with the rest of 'em :)
>
> ms
>
>
> * just the difference between working and not working
>
>
>
> 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 = count >> 2;
> cout << "count = " << count << "\n";
>
> count = count << 2;
> 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
>
>
>
>
> -------------------------------------------------------
> 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
>
|
|
From: Per W. <pw...@ia...> - 2003-09-23 09:06:53
|
Since we are talking about << and >> for shifting bits, I still assume
we are talkinga bout integer arithmetic and not floating point. In that
case * and / won't involve any fp unit.
Anyway - the shift operators are very much wanted now too. It is simpler
to use them that to try and fiddle around with mult/div when you work with
bit manipulation. Yes, it is possible to remove | & ~ ^ << and >> but
manipulation of bit flags wouldn't directly become simpler.
// with bit manipulation
void process_alarms(unsigned alarms) {
unsigned flag = 1;
while (alarms) {
if (alarms & flag) {
cout << "processing alarm " << flag << "\n";
alarms &= ~flag;
}
flag <<= 1;
}
}
// without bit manipulation
void process_alarms(unsigned alarms) {
unsigned mask = 2;
while (alarms) {
mod = alarms % mask;
if (mod) {
cout << "processing alarm " << mod << "\n";
alarms -= mod;
}
mask *= 2;
}
}
/Per W
On Tue, 23 Sep 2003, Michael D. Smith wrote:
>
> OK, with that "slight*" correction. It does multiply and divide by 4 (using
> a 2), and as long as it's small numbers, and not negative numbers, it
> appears to work reliably both ways and back and forth.
>
> 30 or 40 years ago, when computers were a lot slower and had almost no
> memory, using this might have made sense. With a gigahertz computer, you're
> probably much better off to just stick with the regular math functions. If
> they are any slower, it's not by any noticeable amount, they are definitely
> more portable, and much less error prone -- programmer, maintainer, and
> otherwise (the dropped bits).
>
> I'm not real sure that when you engage the math processor to multiply or
> divide by 2,4,8,16 ect, that it doesn't just do this anyway -- only in a
> more portable/reliable fashion.
>
> Bottom line what's the point???
>
> It is fun to play with though. Think about being able to control one little
> bit in hundreds of millions of bytes -- now all I gotta do is figure out
> which one, and what I'm gonna do with the rest of 'em :)
>
> ms
>
>
> * just the difference between working and not working
>
>
>
> 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 = count >> 2;
> cout << "count = " << count << "\n";
>
> count = count << 2;
> 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
>
>
>
>
> -------------------------------------------------------
> 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
>
|