0x01和0x01f之间的区别 [英] Differences between 0x01 and 0x01f

查看:455
本文介绍了0x01和0x01f之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看Identicons的原始源代码。
有一些代码可以提取红色,绿色和蓝色的组件:

  int blue = (代码>> 16)& 0x01f; 
int green =(code>> 21)& 0x01f;
int red =(code>> 27)& 0x01f;

代码变量是一个32位整数。



我的问题是:数字0x01和0x01f之间的区别是什么?



f意味着这个值是一个浮点数,但是为什么使它成为一个浮点数?二进制中的浮点数表达式与整数表达式是不同的吗?如果移植某个特定的语言不使用相同的表示,那么这不会导致便携性原因的问题吗?

另外,由于不了解0x01f问题,我可能读到这个错误,但是这不是将红色,绿色和蓝色表示设置为0或1,取决于最不重要的位?

解决方案

这与浮动无关。这是一个bitmask。

  0x01f = 0b11111 

当你用& >编号时,清除右边第5位左边的所有位。



以下是 0x0a2bfb& 0x01f => 0x00001b

  0x0a2bfb:0000 1010 0010 1011 1111 1011 
0x01f:0000 0000 0000 0000 0001 1111&
----------------------------------------
结果: 0000 0000 0000 0000 0001 1011


I'm looking at the original source code for Identicons. There's a bit of code that does some bit twiddling to extract red, green and blue components:

int blue = (code >> 16) & 0x01f;
int green = (code >> 21) & 0x01f;
int red = (code >> 27) & 0x01f;

The code variable is a 32 bit integer.

My question is this: What's the difference between the number 0x01 and 0x01f?

I'm assuming the f means the value is a float, but why make it a float? Is it that the float representation in binary is different to the integer representation? Wouldn't that cause issues for portability reasons when porting if a particular language doesn't use the same representation?

Also, I'm probably reading this wrong on account of not understanding the 0x01f issue, but isn't this just setting the red, green and blue representations to either 0 or 1, depending on the least significant bit?

解决方案

That has nothing to do with floats. That's a bitmask.

0x01f = 0b11111

When you & your number with it, it clears all the bits to left of the 5th bit from the right.

Here's how 0x0a2bfb & 0x01f => 0x00001b:

0x0a2bfb : 0000 1010 0010 1011 1111 1011
0x01f    : 0000 0000 0000 0000 0001 1111 &
----------------------------------------
result   : 0000 0000 0000 0000 0001 1011

这篇关于0x01和0x01f之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆