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 6549369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:05:04+00:00 2026-05-25T12:05:04+00:00

I have an object that I am storing bits in. class Bitset: def __init__(self,

  • 0

I have an object that I am storing bits in.

class Bitset:
    def __init__(self, bitstring):
        self.bitlist = []
        for char in bitstring:
            self.bitlist.append(int(char))

    def flipBit(self, index):
        val = self.bitlist[index]
        val = (val + 1) % 2
        self.bitlist[index] = val
        self.newBitstring()

    def bitstring(self):
        newString = ''
        for val in self.bitlist:
            newString = newString + str(val)
        return newString

    def __len__(self):
        return len(self.bitlist)

    def __str__(self):
        return self.bitstring()

    def __repr__(self):
        return self.bitstring()

Is there anyway I can convert the bits into a float? Thanks.

  • 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-05-25T12:05:05+00:00Added an answer on May 25, 2026 at 12:05 pm

    Here is a solution that works. as_float32 could be extended to as_float64 by replacing “I” with “L” and “f” with “d”. See the struct documentation for an explanation.

    def as_float32(self):
        """
        See: http://en.wikipedia.org/wiki/IEEE_754-2008
        """
        from struct import pack,unpack
        s = self.bitlist
        return unpack("f",pack("I", bits2int(s)))
    
    # Where the bits2int function converts bits to an integer.  
    def bits2int(bits):
        # You may want to change ::-1 if depending on which bit is assumed
        # to be most significant. 
        bits = [int(x) for x in bits[::-1]]
    
        x = 0
        for i in range(len(bits)):
            x += bits[i]*2**i
        return x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Document object that looks like this: public class Document { public
I have an object, MySession , that has a hashtable for storing arbitrary properties
Hey. I have an object that has a string property called BackgroundColor. This string
I have an object that can build itself from an XML string, and write
I have a C# string object that contains the code of a generic method,
I have a string I need to feed to a com object that has
I have an xml document object that I need to convert into a string.
I have a Java Properties object that I load from an in-memory String ,
I have persistent object, with a string property that often is over 500 charachters.
I have a simple Dictionary(of String, Object) that I need to iterate through and

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.