English 中文(简体)
c++ 浮动浮动的借方
原标题:c++ bit shifting a float
最佳回答
问题回答

如果你想以象惯性那样的方式绕过浮体(或翻了一番),那么你需要直接在浮动点格式中操作。

Short answer: This function will shift the float as if it were an int, and give you the value you expected in your example: (Note: this example relies on Little Endian)

void bitshiftFloating(float &inputData, char inputShift){
    union FloatingParts{
        float input;
        struct {
            unsigned int mantissa : 23;
            unsigned int exponent : 8;
        } parts;
    } U;
    U.input = inputData;
    U.parts.exponent += inputShift;
    inputData = U.input;
    return;
}

执行:

float x = 32.32f;
bitshiftFloating(x,-1); //a shift of -1 will divide it in half, giving you 16.16

www.un.org/Depts/DGACM/index_french.htm

浮标评价为-1^sign * 2^(exponent-127) * 1.(mantissa)。 这意味着,如果你想要“略微转变”,那么你就需要调整其含义。 最容易的方式是将变数变成一个工会,把在比特领域的权宜之计作为自己的变量。 通过对浮动点格式(小端人)的考察,你发现分点/红线为23个轨道,然后是8个,然后是签署点(我们并不真正需要这种情况)。 如下文所示,按同样顺序撰写各栏和随后的轨道领域(确保它们重新组合所有类型)。 这将确保适当储存大量资金。

(说明:are, 采取其他方式,但easiest>。 你们不得不自己做检测,看看看它是否真正比标准分配更快,以备你们使用。 如果你重新使用大比尼安,轨道领域将有所不同。

This is what the union should look like:

union FloatingParts{
            float input;    //container for whole variable
            struct {    //struct for bit-field array; float = -1^sign * 2^(exponent-127) * 1.(mantissa)
                unsigned int mantissa : 23; //fraction data; extract to a dummy union to get real value, or manually: 1+(bit1*2^1,...bit23*2^23)
                unsigned int exponent : 8;  //biased, so -127 to get real value.
                unsigned int sign : 1;  //sign bit.
            } parts;
        }floatUnion;

And then initialize it by setting floatUnion.input to your desired value. You can then call floatUnion.parts.exponent, and add or subtract the number you would bit shift by, as it s an exponent of 2:

floatUnion.input = 32.32f;
floatUnion.parts.exponent += -1;

结果是16.16f

铭记C++中的工会是技术上的 UB,尽管大多数汇编者与它们没有关系。 它还没有避免重复,因为工会是自己的变量。 为了避免重复,你首先必须在工会内建立浮标。

Edit: As someone pointed out, endianness is important here, so the bit fields might be different depending on your platform or implementation. The examples here are in Little Endian.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签