question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reversal of .bin()

See original GitHub issue

I’m wondering if there is a way to revert calling .bin(). Something like:

x1 = Fxp(3.4)
x_bin = x1.bin()
x2 = from_bin(x_bin)
assert x1 == x2

My use case is to convert into the notation, do some operations, then convert the result back over. I see the constructor can take in a binary value but it looks like it only takes in a “normal” python bit string. Is this possible currently?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
francof2acommented, Dec 23, 2021

Yes, it would be good! I’ll try to include that in next release. Thanks for the recommendation.

0reactions
francof2acommented, Feb 14, 2022

As you are using 2 bits for fractional part, 256.2 is rounded to 256.0. You need more fractional bits to get a fixed-point value closer to 256.2.

With 2 bits for fractional part, the closer values to 256.2 are 256.0 and 256.25. If you use rounding='ceil' or rounding='around', you will get a fixed-point value of 256.25 instead of 256.0.

ret_int = Fxp(float_val, n_word=32, signed=True, n_frac=2, n_int=29, rounding='ceil')

If you fix the n_word size to 32 but let to Fxp finds the best fractional part:

ret_int = Fxp(float_val, n_word=32, signed=True)

fxp-s32/22(256.19999980926514)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reverse actual bits of the given number - GeeksforGeeks
Given a non-negative integer n. The problem is to reverse the bits of n and print the number obtained after reversing the bits....
Read more >
Slick way to reverse the (binary) digits of a number in Python?
int(reversed(s),2) == f(int(s,2)) whenever s is a string of zeros and ones starting with 1. Right now I am using lambda x: int(''.join(reversed(bin(x)[2:])),2)....
Read more >
Simple Tools and Techniques for Reversing a binary - bin 0x06
Exploring and comparing some common tools and techniques for reversing binaries.https://github.com/radare/radare2-=[ Stuff I use ] ...
Read more >
Python: Reverse the binary representation of an integer and ...
def test(n): return int(bin(n)[::-1][:-2], 2) n = 13 print("Original number: ", n); print("Reverse the binary representation of the said ...
Read more >
Python program to reverse bits of a positive integer number?
First convert number into binary using bin() function. Then skip the first two character of binary representation because bin() appends 0b ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found