Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:47:12+00:00 2026-06-08T16:47:12+00:00

Given a byte b of 8 bits, the following formula returns the bits of

  • 0

Given a byte b of 8 bits, the following formula returns the bits of b “swapped” (0 and 7 are swapped, 1 and 6 are swapped, etc.):

(b * 0x0202020202 & 0x010884422010) % 1023

When I have a function implementing this hack, namely

def reverseBits(b):
    return (b * 0x0202020202 & 0x010884422010) % 1023

then I get an overflow:

OverflowError: cannot fit ‘long’ into an index-sized integer

How can I implement the bit-swapping hack in Python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:47:18+00:00Added an answer on June 8, 2026 at 4:47 pm

    This seems to work as you expect:

    def sb(b):
        return (b * 0x0202020202 & 0x010884422010) % 1023
    
    def harness(i):
        print '{:4}: {} -> {}'.format(i,bin(i)[2:].zfill(8),bin(sb(i))[2:].zfill(8))    
    
    for i in range(256):
        harness(i)
    

    Prints:

       0: 00000000 -> 00000000
       1: 00000001 -> 10000000
       2: 00000010 -> 01000000
       3: 00000011 -> 11000000
       4: 00000100 -> 00100000
       5: 00000101 -> 10100000
       6: 00000110 -> 01100000
       ...
     248: 11111000 -> 00011111
     249: 11111001 -> 10011111
     250: 11111010 -> 01011111
     251: 11111011 -> 11011111
     252: 11111100 -> 00111111
     253: 11111101 -> 10111111
     254: 11111110 -> 01111111
     255: 11111111 -> 11111111
    

    This algorithm (and others) are found here. As stated, this method only works for a BYTE, so you will need to use another method for a larger bit pattern.

    Edit

    BTW: You can do bit reversal without any fear of overflow or math problems and for larger bit fields by using string manipulation:

    >>> w=32
    >>> s=bin(1234567)[2:].zfill(w)
    >>> rb=s[::-1]
    >>> s
    '00000000000100101101011010000111'
    >>> rb
    '11100001011010110100100000000000'
    

    Then convert back to an int this way:

    int(rb,2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following information Public Enum Request As Byte None = 0 Identity =
Given that the Windows API function GetGlyphIndices() can translate a 2 byte UNICODE char
I have been given the following programming task (edited to obscure mission-specifics): The raw
Given a byte array with a length of two we have two possibilities for
What is the best way in Java to print a gif given as byte[]
I need to write effective and quick method to search byte array for given
Given that the web application doesn't have su privileges, I'd like to execute a
This code: BitArray bits = new BitArray(new byte[] { 7 }); foreach (bool bit
I have an API that gives me PCM wave data: http://msdn.microsoft.com/en-us/library/ff966424.aspx The byte[] buffer
As we all know that java uses the following data types byte Occupy 8

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.