简体   繁体   中英

Converting Binary Integer into Binary Fraction

I am generating integers (let's call them "m_i" where i is an integer). For example, I may generate m_5 = 31 (=11111). There is one restriction on each of the m_i and that is that each m_i must be an odd integer and must be less than 2^i. The reason for this is that I am actually interested in generating the values v_i where v_i = m_i / (2^i). (For anyone interested, this is for a Sobol sequence generator)

Now, m_i is just the binary expansion of v_i after the decimal point (eg v_5 = 0.11111) and ideally I would like to be able to simply right shift m_i by i bits to get v_i. However, this is, of coarse, illegal in c++ because I cannot use bit operations of floats or doubles.

Right now I am using a quick implementation of integer power and simply using the line:

v[i] = ((double)m[i]) / ((double)ipow(2,i+1));

And this works fine but this is an O(log n) calculation, when realistically, there should be a way of doing this in O(1). Are there any wizards out there who might be able to let me know if there is a better way of achieving the desired result?

Thanks in advance for the help!

多亏了顺磁牛角包

m[i] / (1 << i + 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM