473,385 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3289
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 I'm trying to do is use a variable to hold a bitmask...
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 variables without using any other variable. Use one...
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", (x^((~((x>>31)&1))+1)) + ((x>>31)&1) ); That works, but it...
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 example short int *ptr; short int q; int final;
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 implemented on different kinds of machines -- say, those...
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 other c derivitives but ANSI C is making me loose my...
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 sort of example application of them. Call me what...
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... Imagine a SQL Server 2005 database with a table with an...
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 unequipped to do so. I've never done much with bitwise...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.