C语言中的位运算 [英] Bit operations in C

查看:81
本文介绍了C语言中的位运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,如果我有一个32位整数.前28位(从左开始)用于存储内存块的大小,后两位为0,后两位为:

So if I have an integer that is 32 bits. The first 28 bits (from left) are to store the size of a memory chunk, the next two are 0s and the last two are:

  • 存储,如果它是最后一个节点,然后
  • 分别存储是否使用.

我想做的是知道如何在isLast操作和isUsed操作上打开和关闭该标志.

What I am trying to do is to know how to turn the flag on and off on the isLast operation and the isUsed operation.

(如果仅考虑最后两个整数(再次,我们从左开始),则01将不是最后一个并被使用,例如,另一个例子是11被最后一个并被使用,00不是最后一个且未被使用. )

(If we consider only the last two integers (again, we start left) then 01 would be not last and is used for example, one more example is 11 is last and is used, 00 is not last and not used.)

我希望能够以一种简单的方式打开和关闭这些标志.我知道我将需要使用&等位操作和|但我不确定如何.

I want to be able to turn the flags on and off in an easy way. I know I will need to use bit operations including & and | but I am not sure how.

如果您需要更多有关问题的描述,请问我问题.

Please ask me questions if you need more description of the problem.

推荐答案

//turn on isUsed
data |= 1;
//turn off isUsed
data &= ~1;
//turn on notLast
data &= ~2;
//turn off notLast
data |= 2;

这篇关于C语言中的位运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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