home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18268 < prev    next >
Encoding:
Internet Message Format  |  1992-12-12  |  1015 b 

  1. Path: sparky!uunet!portal!cup.portal.com!Aurelius
  2. From: Aurelius@cup.portal.com (Mark Christian Barnes)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to do an efficient ROTATE in C?
  5. Message-ID: <71576@cup.portal.com>
  6. Date: Sat, 12 Dec 92 12:49:03 PST
  7. Organization: The Portal System (TM)
  8. Distribution: ba
  9. References:  <1992Dec9.033715.3807@kronos.arc.nasa.gov>
  10. Lines: 21
  11.  
  12. Eric A. Raymond writes:
  13.  
  14. |C has those lovely >> and << ops for shifting, but no built in rotate
  15. |operators?
  16. |Anyone have an efficent way to trick a compiler into generating a
  17. |rotate? (either direction is fine).
  18.  
  19.   Most C compilers will do rotates, i.e. logical shifts, using
  20. the << and >> operators, when the type of argument is UNSIGNED.
  21. Otherwise arithmetic shifts must be used to preserve the sign
  22. of the argument.
  23.  
  24.    {
  25.     int      sign  = -1;
  26.     unsigned unsign = 1;
  27.  
  28.     sign   = sign   << 2; /* this should be an arithmetic shift */
  29.     unsign = unsign << 2; /* this should be a logical shift */
  30.    }
  31.  
  32.             Regards, Aurelius@cup.portal.com
  33.