472,353 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Bitwise expression

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
5 3209
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

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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What...
10
by: littlehobo | last post by:
I'm trying to figure out this question i was asked but am having no luck! here's the question: "Write a Program that swaps the contents of two...
34
by: Christopher Benson-Manica | last post by:
I'm trying to compute the absolute value of an integer using only bitwise operators... int x; sscanf( "%d", &x ); printf( "%d\n",...
4
by: Che | last post by:
Hi, I am just curious if anybody ever used a pointer to an integer(any datatype) and then manipulate the same with bitwise operators. For...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be...
3
by: shdwsclan | last post by:
I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any...
2
by: Mark Rae | last post by:
Hi, This isn't *specifically* an ASP.NET question, so I've also posted it in the ADO.NET group - however, it's not too far off-topic... ...
10
by: Rob Wilkerson | last post by:
I'm attempting to do some work around existing code that uses bitwise operations to manage flags passed into a function and I'm quite frankly...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.