给定两个 float A 和 B,它们是命令行参数,我必须创建方法来对它们进行按位运算。包括 And、Or、Not、xOr、浮点加法等...
如何访问 C 中的每一位?我需要访问特定的 1 和 0。知道怎么做吗?
最佳答案
这是一个使用 union 的示例,正如 Keith 所建议的那样。
/* -std=c99 */
#include <stdio.h>
int main(int argc, char * argv[]) {
/* note, I'm assuming an int has the same size of a float here */
assert( sizeof(unsigned int) == sizeof(float) );
union {
float floating;
unsigned int integer;
} a;
a.floating = 3.14;
/* prints the bits in reverse order */
unsigned int temp = a.integer;
for( int i = 0; i < sizeof(float)*8; i++ ) {
printf("%d", temp & 0x1);
temp = temp >> 1;
}
}
编辑:将有符号整数更改为无符号整数。
关于c - C 中的位,如何访问 C float 中的底层位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9401726/