# Bitwise expression

mensanator at aol.com mensanator at aol.com
Tue Jan 9 01:30:35 CET 2007

```Gigs_ 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
>
>
> thanks people

Here's some examples:

##    What is BINARY?
##
##     n   base 2
##    -- --------
##     0 00000000
##     1 00000001
##     2 00000010
##     3 00000011
##     4 00000100
##     5 00000101
##     6 00000110
##     7 00000111
##     8 00001000
##     9 00001001
##    10 00001010
##    11 00001011
##    12 00001100
##    13 00001101
##    14 00001110
##    15 00001111
##
##
##    What does << do?
##
##        00000111
##     << 00000100
##    ------------
##        01110000
##
##    What does >> do?
##
##        00100100
##     >> 00000010
##    ------------
##        00001001
##
##    What does & do?
##
##        00011011
##      & 00001001
##    ------------
##        00001001
##
##        00011011
##      & 00001110
##    ------------
##        00001010
##
##    What does | do?
##
##        00010001
##      | 00001010
##    ------------
##        00011011
##
##        00011011
##      | 00010001
##    ------------
##        00011011
##
##    What does ^ do?
##
##        00011011
##      ^ 00011111
##    ------------
##        00000100
##
##        00011111
##      ^ 00001110
##    ------------
##        00010001
##
##    Bitwise demo: the Collatz Conjecture
##
##    41 31 47 71 107 161 121 91 137 103 155 233
##    175 263 395 593 445 167 251 377 283 425 319
##    479 719 1079 1619 2429 911 1367 2051 3077
##    577 433 325 61 23 35 53 5 1

bitwise.py

import gmpy  # has lots of neat bitwise operations

def convert_to_binary(n,bits):
s = gmpy.digits(n,2)       # base 2 conversion
return s

def report(n,m,o,p):
print '    %s' % (n)
print '%3s %s' % (o,m)
print '------------'
print '    %s' % (p)
print

def Collatz(n):
# if n is even, divide by 2
# if n is odd, multiply by 3 and add 1
# Collat Conjecture: n always reaches 1
while n>1:
# find bit position of LS 1 bit
f = gmpy.scan1(n)
if f == 0:       # then n is odd
n = n*3 + 1
else:            # n is even
# remove all factors of 2 in one fell swoop
n = n >> f
print n,
print

print 'What is BINARY?'

print """
n   base 2
-- --------"""
for n in xrange(16):
print '%2d %s' % (n,convert_to_binary(n,8))

print
print

print 'What does << do?'
print

report(convert_to_binary(7,8), \
convert_to_binary(4,8), \
'<<', \
convert_to_binary(7<<4,8))

print 'What does >> do?'
print

report(convert_to_binary(36,8), \
convert_to_binary(2,8), \
'>>', \
convert_to_binary(36>>2,8))

print 'What does & do?'
print

report(convert_to_binary(27,8), \
convert_to_binary(9,8), \
'&', \
convert_to_binary(27&9,8))

report(convert_to_binary(27,8), \
convert_to_binary(14,8), \
'&', \
convert_to_binary(27&14,8))

print 'What does | do?'
print

report(convert_to_binary(17,8), \
convert_to_binary(10,8), \
'|', \
convert_to_binary(17|10,8))

report(convert_to_binary(27,8), \
convert_to_binary(17,8), \
'|', \
convert_to_binary(27|17,8))

print 'What does ^ do?'
print

report(convert_to_binary(27,8), \
convert_to_binary(31,8), \
'^', \
convert_to_binary(27^31,8))

report(convert_to_binary(31,8), \
convert_to_binary(14,8), \
'^', \
convert_to_binary(31^14,8))

print 'Bitwise demo: the Collatz Conjecture'
print
Collatz(27)

```