5

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.

5
  • I'm not sure what you mean by "Convert the bits into a float". Do you want to do the equivalent of a C typecast (i.e. have the system interpret the sequence of bits as if it were a float)? Also, if this is a homework problem, you should add the [homework] tag. Commented Sep 13, 2011 at 1:22
  • Thank you for understanding. It is essentially a C typecast. This is not a homework problem though. I just have no idea how to do this. Commented Sep 13, 2011 at 1:24
  • How do you want to interpret them as a float? As a number between 0 and 1? Two's compliment? You need to be more specific? struct only works for floats in IEEE 754 binary format. Commented Sep 13, 2011 at 1:36
  • I would like to interpret them using the IEEE 754 format. When I try using struct to unpack it I am told that the string can only be 4 characters long. Doesn't the standard require 32 bits? Commented Sep 13, 2011 at 1:44
  • You are not storing bits. You are storing integers that happen to have the values 0 or 1. Forget this at your peril. Commented Sep 13, 2011 at 2:04

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

I found a few errors when I adapted the code from my spyder window to something that should work in your object. They should be corrected now.
5

There are libraries that can do all this for you if you don't want to reinvent the wheel. My bitstring library could help:

>>> from bitstring import BitArray
>>> a = BitArray(float=0.34, length=32)
>>> a.bin
'00111110101011100001010001111011'
>>> a.float
0.3400000035762787
>>> a.bin = '01001001011101000010010000000000'
>>> a.float
1000000.0
>>> a.bytes = 'helloall'
>>> a.float
7.819486525216155e+194

The float interpretation is only allowed when there are either 32 or 64 bits; take a look at the documentation for more details.

2 Comments

Useful library to convert between IEEE754 and binary32 notation
it adds extra digits in the float representation. The original number is 0.34. How to keep it as 0.34?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.