Bitwise expression

Gigs_ gigs at hi.t-com.hr
Tue Jan 9 06:59:31 EST 2007


Hendrik van Rooyen wrote:
> "Gigs_" <gigs at hi.t-com.hr> wrote:
> 
> 
>> Can someone explain me bitwise expression?
>> few examples for every expression will be nice
>>
>> x << y Left shift
>> x >> y Right shift
>> x & y Bitwise AND
>> x | y Bitwise OR
>> x ^ y Bitwise XOR (exclusive OR)
>> ~x Bitwise negation
> 
> The short, and possibly weird, but true, answer is:
> 
> If you have to ask this question, you should avoid
> using these things. - Think of them as "Advanced Magic"
> 
> But this is unhelpful, so a slightly longer answer is:
> 
> Computer memory is like a long string of flip-flops that can
> take on one of two states - "on" or "True" represented normally by
> a digit 1, and "off" or "False" - a digit 0.   Hence the term binary.
> 
> Binary means "two valued", just like a Bicycle has two wheels.
> 
> These flip-flops are the smallest element of memory, and one
> of them is called a "bit".  The plural is "bits".  "plural" means
> "more than one of".
> 
> Eight bits are called a Byte.
> Half a Byte is a Nibble - four bits (sometimes spelt Nybble by
> people who are trying to be cute).
> There is a concept called a "Word" of memory which is ill defined.
> Sometimes it is one or two Bytes, sometimes three nibbles, sometimes
> four, eight or sixteen bytes - depends on the hardware's bus width.
> 
> No, I am not going to explain what a bus is.
> 
> You can think of a python number as a word of eight bytes long,
> and a python string as a number of bytes of arbitrary length.
> 
> I am also not going to explain big and little endian representation.
> Yahoo for it.
> 
> Now the logic operators, as applied to nibbles:
> 
> 1110  <<  0111               - shifted left, filled with zero from right
>                                         (multiply by two - seven to fourteen)
> 0010  >>  0100               - shifted right, filled with zero from left
>                                         (divide by two - four to two)
> 0100  =    0101 & 1110   - true if both bits true, false otherwise
>                                         (used for masking, clearing bits)
> 1101  =    0101  |  1100   - false if both bits false, true otherwise
>                                         (True if either bit true - used to set
> bits)
> 1001  =    0101  ^ 1100   - true if only one of the bits true, else false
>                                         (anything xored with itself is all zero)
>                                         (used to toggle bits, identity testing)
> 1001  ~    0110               - inversion "not"  - not true is false, not false
> is true
> 
> Also yahoo for "Boolean algebra" and "De Morgan" and "IEEE floating point
> representation"
> 
> hth - Hendrik
> 
> 

hey I know about bit, bits things, just didn't know how to use bitwise 
expression (didn't try it and didn't know what it means). so I just 
needed some examples.
Now is all clearer thanks to mensanator at aol.com and Hendrick van Rooyen



More information about the Python-list mailing list