By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,526 Members | 2,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,526 IT Pros & Developers. It's quick & easy.

Bitwise expression

P: n/a
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
Jan 8 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Mon, 08 Jan 2007 22:32:12 +0100, Gigs_ wrote:
Can someone explain me bitwise expression?
few examples for every expression will be nice
http://wiki.python.org/moin/BitwiseOperators
--
Richard
Jan 8 '07 #2

P: n/a

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
# not found in standard Python

def convert_to_binary(n,bits):
s = gmpy.digits(n,2) # base 2 conversion
s = '0'*(bits-len(s)) + s # add leading 0's
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)

Jan 9 '07 #3

P: n/a
"Gigs_" <gi**@hi.t-com.hrwrote:

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
Jan 9 '07 #4

P: n/a
Hendrik van Rooyen wrote:
"Gigs_" <gi**@hi.t-com.hrwrote:

>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 me********@aol.com and Hendrick van Rooyen
Jan 9 '07 #5

P: n/a
"Gigs_" <gi**@hi.t-com.hrwrote:

Now is all clearer thanks to me********@aol.com and Hendrick van Rooyen
Contrary to popular belief in the English speaking world -
>>"c" in "Hendrik"
False
>>>
There is no "c" in "Hendrik"

: - ) - Hendrik

Jan 10 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.