简体   繁体   中英

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. 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). In that case you would change the + 1 with + 2 in the code above.

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