473,544 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3301
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_bina ry(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_b inary(n,8))
print
print

print 'What does << do?'
print

report(convert_ to_binary(7,8), \
convert_to_bina ry(4,8), \
'<<', \
convert_to_bina ry(7<<4,8))

print 'What does >do?'
print

report(convert_ to_binary(36,8) , \
convert_to_bina ry(2,8), \
'>>', \
convert_to_bina ry(36>>2,8))

print 'What does & do?'
print

report(convert_ to_binary(27,8) , \
convert_to_bina ry(9,8), \
'&', \
convert_to_bina ry(27&9,8))

report(convert_ to_binary(27,8) , \
convert_to_bina ry(14,8), \
'&', \
convert_to_bina ry(27&14,8))

print 'What does | do?'
print

report(convert_ to_binary(17,8) , \
convert_to_bina ry(10,8), \
'|', \
convert_to_bina ry(17|10,8))

report(convert_ to_binary(27,8) , \
convert_to_bina ry(17,8), \
'|', \
convert_to_bina ry(27|17,8))

print 'What does ^ do?'
print

report(convert_ to_binary(27,8) , \
convert_to_bina ry(31,8), \
'^', \
convert_to_bina ry(27^31,8))

report(convert_ to_binary(31,8) , \
convert_to_bina ry(14,8), \
'^', \
convert_to_bina ry(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
2520
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 of 'flags' for users on a website. The function below is supposed to be a search function for one of those flags in a particular thing. The...
10
8211
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 of the bitwise operators." I'm clueless... Any help would be appreciated! Thanks.
34
16819
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 assumes 32 bit integers. Is there a portable/standard way to do this? Or are ANSI integers always 32 bits? If not, is using sizeof(int) * 8 - 1 as...
4
7516
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
3453
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 with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic...
3
12485
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 hair....especially ANSI C's bitwise operators..... For reference i come from the (Java/C#/C++) realm and was never forced to use these. Many people...
5
5768
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 you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not...
2
2736
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 int column used for bitwise data - you know the sort of thing... 0, 1, 2, 4, 8, 16 etc intBitwise strName ----------------------
10
1759
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 operations and all the reading I've done today doesn't appear to be helping me much. I'm hoping someone here can provide a remedial lesson. ...
0
7424
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7365
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7607
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7376
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5909
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4918
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
661
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.