简体   繁体   English

浮动右移

[英]right shift for float

I have float var like that 我有这样的浮动变量

float f = 0b 00000000 11110001 00000000 00000000 

I want to take 1st(not 0st) byte to char variable. 我想将第1个(非第0个)字节带给char变量。 I can't do << and >>. 我不能做<<和>>。 how can i do that? 我怎样才能做到这一点?

There is generally little point messing with the binary representation of floating point values. 通常很少有点与浮点值的二进制表示混淆。 Any you'll try will not be portable. 您将尝试的任何内容都无法移植。 However, generally, these two work: 但是,通常,这两个工作:

char c(reinterpret_cast<char*>(&f)[1]);
union {
    float f;
    char  c[sizeof(float)];
} u = { f };
u.c[1];
char bla;
bla = *((char *) &f + 1)

Also remember that with endianness, on little endian systems what you may actually want is byte 2 (assuming you count your byte from 0 to 3). 还要记住,采用字节序,在小字节序系统上,您实际需要的是字节2(假设您将字节从0计数到3)。 In that case you would change the + 1 with + 2 in the code above. 在这种情况下,您可以在上面的代码中将+ 1更改为+ 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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