shift right arithmetic

Theodore A. Camus tac at cs.brown.edu
Thu Sep 6 12:29:50 AEST 1990


I would like to do a portable arithmetic right shift in C.


Possible solutions and problems :

1) '>>' will _not_ guarantee the result on a signed argument (ANSI & K&R)

2) dividing by a power of 2 will often compile to arithmetic shifts, but 
   only for the smaller powers of 2, not for the full 2^1 - 2^31 shift range.

3) One could test for a negative argument, and if it was negative, use a
   negate / shift / negate sequence, but there should be a better way.


For most compilers, '>>' will differentiate between signed and unsigned
arguments, but again K&R and ANSI allows it to be implementation-defined.

Any answers ?

        - Ted


  CSnet:     tac at cs.brown.edu                          Ted Camus  
  ARPAnet:   tac%cs.brown.edu at relay.cs.net             Box 1910 CS Dept
  BITnet:    tac at browncs.BITNET                        Brown University
  "An ounce of example is worth a pound of theory."    Providence, RI 02912



More information about the Comp.lang.c mailing list