Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Zero-fill shift

Thread Tools

Zero-fill shift

Scott David Daniels
Posts: n/a
Eli Stevens (WG.c) wrote:

> Daniel Orner <(E-Mail Removed)> wrote:
>>Great, this works. Unfortunately, I've run into another problem.
>>In the Java algorithm, two integers are added. This often results in
>>an overflow and a negative number, which is the desired result....

> But more to the point: Here's something that will do the trick if your
> overflow isn't ever more than one bit....

And if you want to be able to handle multibit overflow:

SIGN_BIT = 2 ** 7 # 31 if you prefer 32-bit signed numbers

def wrap(n):
if n & SIGN_BIT:
return n | -SIGN_BIT
return n & (SIGN_BIT - 1)

-Scott David Daniels Removed)
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java left shift and right shift operators. Sanny Java 38 04-29-2011 10:02 PM
what "shift" does, if not "$_ = shift;" ? devphylosoff Perl Misc 3 12-04-2007 12:27 AM
Left Shift / Right Shift Operators Santosh Nayak C Programming 16 11-30-2006 05:10 PM
Shift - byte[] buf shift Roberto Gallo Java 3 01-27-2004 04:26 PM
left shift then right shift an unsigned int Wenjie C++ 3 07-11-2003 08:22 PM