473,659 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bit shifting question

Hi,
My background is c/c++ and java. I'm learning python at this point.

My question is does python share java's peculiar mode of bit shifting, or
does python adhere closer to c's bit shifting?

in java there are 3 kinds of bit shifts:
<< (shift left)
(preserve the sign bit as we move right )
(0 filled on the left as we move right)

In C, the behavior is
<< (shift left) (shift right , 0 filled (like java's >>>'))


I tried looking around but haven't really got an answer yet. I guess I need
to write a mini python script. Please don't write any code, I'm just
looking for a website or something where I can figure this stuff out in
python.

Thanks in advance,

David
-------
Cell: http://cellphone.duneram.com/index.html
Cam: http://www.duneram.com/cam/index.html
Tax: http://www.duneram.com/index.html

_______________ _______________ _______________ _______________ _____
FREE pop-up blocking with the new MSN Toolbar – get it now!
http://toolbar.msn.click-url.com/go/...ave/direct/01/
Jul 18 '05 #1
6 4397
"David Stockwell" <wi*******@hotm ail.com> writes:
Hi,
My background is c/c++ and java. I'm learning python at this point.

My question is does python share java's peculiar mode of bit shifting,
or does python adhere closer to c's bit shifting?
The right shift in Python is arithmetic, i.e. preserves the sign bit.
in java there are 3 kinds of bit shifts:
<< (shift left)
>> (preserve the sign bit as we move right )
>>> (0 filled on the left as we move right)
In C, the behavior is
<< (shift left) >> (shift right , 0 filled (like java's >>>'))
Have you tried this recently? I believe that whether >> sign fills is
undefined by the C standard but most of the time it dos.
I tried looking around but haven't really got an answer yet. I guess
I need to write a mini python script.


No you don't! If you haven't realized that you can do

$ python
Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright" , "credits" or "license" for more information. (-1) >> 3

-1

you have MUCH to learn about Python...

Cheers,
mwh

--
Python enjoys making tradeoffs that drive *someone* crazy <wink>.
-- Tim Peters, comp.lang.pytho n
Jul 18 '05 #2
On 2004-05-17, Michael Hudson <mw*@python.net > wrote:
My question is does python share java's peculiar mode of bit
shifting, or does python adhere closer to c's bit shifting?


The right shift in Python is arithmetic, i.e. preserves the
sign bit.


Which one is the sign bit? IOW, how wide is a "word"?

--
Grant Edwards grante Yow! LBJ, LBJ, how many
at JOKES did you tell today??!
visi.com
Jul 18 '05 #3
Michael Hudson wrote:
Have you tried this recently? I believe that whether >> sign fills is
undefined by the C standard but most of the time it dos.


It's implementation defined, to be more exact.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ But you're not going to be there tomorrow. And it's all about
tomorrow. -- Montgomery Brogan
Jul 18 '05 #4
Grant Edwards wrote:
On 2004-05-17, Michael Hudson <mw*@python.net > wrote:
My question is does python share java's peculiar mode of bit
shifting, or does python adhere closer to c's bit shifting?


The right shift in Python is arithmetic, i.e. preserves the
sign bit.


Which one is the sign bit? IOW, how wide is a "word"?

In most cases, you should not be able to discover how wide
a "word" is. Python is trying to move to a single integer
data type which is a combination of the current "long" and
"int" where the internal representation is an implementation
detail. Unfortunately, shifting is one of those places you
can still discover the "width" with code like:

def wordwidth():
for shift in range(256):
if 1 << shift < 0:
return 1 + shift

--
-Scott David Daniels
Sc***********@A cm.Org
Jul 18 '05 #5
In article <40********@nnt p0.pdx.net>,
Scott David Daniels <Sc***********@ Acm.Org> wrote:
Unfortunately, shifting is one of those places you
can still discover the "width" with code like:

def wordwidth():
for shift in range(256):
if 1 << shift < 0:
return 1 + shift


Of course this will give a warning in 2.3 and fail to work in 2.4...
I wish I could get the 2.4 behavior now with a from __future__ import
but I don't see one for this, instead I find myself doing 1L<<shift a
lot.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #6
Grant Edwards <gr****@visi.co m> writes:
On 2004-05-17, Michael Hudson <mw*@python.net > wrote:
My question is does python share java's peculiar mode of bit
shifting, or does python adhere closer to c's bit shifting?


The right shift in Python is arithmetic, i.e. preserves the
sign bit.


Which one is the sign bit? IOW, how wide is a "word"?


Well, quite :-) Perhaps I should have said "preserves sign".

Cheers,
mwh

--
I'm sorry, was my bias showing again? :-)
-- William Tanksley, 13 May 2000
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
8733
by: GGG | last post by:
Noticed something odd in the way bit shifting was working today. As far as I have ever heard, shifting will shift in zeros(signed ints aside) However I foudn something odd when I am shifting exactly the number of bits in a value... i.e. uint32_t x = 5;
8
3665
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in that it copies one block of data to another block of data until the block that is being copied contains a zero/null. the difference with this code is that it's doing 4bits at a time (all the values are 4bits) and the two blocks of data may not be...
2
2055
by: salsipius | last post by:
Can someone please help me clarify the below code. I think the shifting has to do with converting datatypes and/or loss of data but am not really clear on the details, could you help shed some light please? // Allocate array for( i = 0; i < Length; i++ ) { //pArray_00 is a BYTE Array -- Here a cast is used because
22
5356
by: Sekhar | last post by:
can anyone shed some light on "Bit Shifting" of structures? I have a doubt whether i am using the right key word or not.
10
10688
by: krunalb | last post by:
Hi, I am trying to shift unsigned long long value by 64 bits and this is what i get #include <stdio.h> int main() { unsigned short shiftby= 64;
20
2502
by: Charles Sullivan | last post by:
I understand different processor hardware may store the bits in a byte in different order. Does it make a difference in C insofar as bit-shifting unsigned char variables is concerned? E.g, if I have unsigned char x = 1; is it always true that (x << 1) == 2 (x << 2) == 4 etc?
16
1550
by: lak | last post by:
i know left and right shift normally,but i cant know what happens if it is negative. for example int x=-2; x<<=1;//what happens here
4
1864
by: Neil | last post by:
I previously posted about data shifting between records in my Access 2000 MDB with a SQL Server 7 back end, using ODBC linked tables. Every once in a while, data from one record mysteriously appears in another record. This incident happened again, this time adding a new wrinkle to the situation. There are two tables -- TableA and TableB -- which have a one-to-one relationship with each other, joined on TableA's autonumber primary key...
12
2252
by: Boltar | last post by:
I seem to be having yet more wierd issue with bit shifting. It seems the following code doesnt do anything under gcc (ie it returns -1 as both results). Anyone know why? Is it another language definition or CPU issue? main() { printf("%d\n",(int)0xFFFFFFFF >1); printf("%d\n",(int)-1 >1); }
1
8525
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.