将浮点数赋给unsigned char时发生了什么? [英] what happened when assign a float to unsigned char ?
本文介绍了将浮点数赋给unsigned char时发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
float f = -1;
unsigned char c = f;
return 0;
}
我检查& f和& c的内容如下:
I check the contents of &f and &c as follows:
(gdb) x/xw &f
0x7fffffffe2b8: 0xbf800000
(gdb) x/xw &c
0x7fffffffe2bf: 0x000000ff
为什么& c看起来像这样?
why does &c look like this ?
推荐答案
转换-1
浮点到无符号字符,它被强制转换为0xffffffff
的整数,所以将它存储在一个字符(字节)中给出0xff
,如您所见。
Converting-1
in floating point to an unsigned character it is coerced to an integer which would be0xffffffff
, so storing that in a character (byte) gives0xff
, displayed as you see.
这篇关于将浮点数赋给unsigned char时发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文