How to reverse bits...

Stan Brown, Oak Road Systems browns at astro.pc.ab.com
Thu Aug 16 02:20:56 AEST 1990


In article <1990Aug14.233308.27889 at caen.engin.umich.edu>, jal at acc (John Lauro) writes:
> In article <1990Aug14.203212.16248 at motcad.portal.com> jtc at motcad.portal.com (J.T. Conklin) writes:
>>Another useful table is one that flips the bits 1-for-0,0-for-1.  This
>>is useful for inverting an image.
> 
> How about:   (assuming 8 bit char c) 
>    c=255-c;  or
>    c^=255;
> 
>     - John_Lauro at ub.cc.umich.edu


Please, no magic numbers.  You can use the bitwise-complement operator, ~ tilde
(pronounced, "squiggle").

	c = ~c

It works on any numeric type, and the result has the same type as the operand.

Typical application: to sset all the bits in n unsigned word:

	unsigned all_bits_set = ~(unsigned)0;
-- 

Stan Brown, Oak Road Systems, (216) 371-0043
The opinions expressed are mine. Mine alone!  Nobody else is responsible for
them or even endorses them--except my cat Dexter, and he signed the power of
attorney only under my threat to cut off his Cat Chow!



More information about the Comp.lang.c mailing list