dongzuo7166 2013-01-15 22:39
浏览 56

Bitwise 7 | ~5 = -1; 真的吗?

I've been trying to figure out bitwise operators again today, and some things are still a little fuzzy to me. Everything made sense until I started trying to combine the | operator with ~, for instance: number | ~number. On a side note, I already read the wikipedia article on the two's complement, and understand that aspect of it (I think).

------------------------------------------
| 128 | 64 | 32 | 16 |  8 |  4 |  2 |  1 |
------------------------------------------
|   0 |  0 |  0 |  0 |  0 |  1 |  1 |  1 | = 7
------------------------------------------
|   1 |  1 |  1 |  1 |  1 |  0 |  1 |  0 | = ~5 or -6
------------------------------------------

Using the & operator like 7 & ~5 yields the expected result of 2, as only the 2 column is set in both rows. What's getting me here is that 7 | ~5 is outputting -1. I'd expect that would either be 255, because every column has a 1 in it, or 5, because the bottom row might count 0s instead of 1s. But it's neither of those, it's -1.

I've googled this until my brain hurts, and I can't find one single article anywhere that addresses this. Does anyone know what's going on here?

  • 写回答

1条回答 默认 最新

  • douhan8892 2013-01-15 22:40
    关注

    The result of 7 | ~5 is a bit pattern of all ones. That's -1 in two's complement.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么