C bit 操作

C bit 操作C 位 操作 一 C bit 操作 C 语言 二进制位 操作 1 Setting a bit Use the bitwise OR operator to set a bit number 1 lt lt x That will set bit x 2 Toggling a bit

大家好,我是讯享网,很高兴认识大家。

C 位 操作

一、C bit 操作(C语言 二进制位 操作)

1.Setting a bit
Use the bitwise OR operator (|) to set a bit.

number |= 1 << x;

讯享网

That will set bit x.

2.Toggling a bit

The XOR operator (^) can be used to toggle a bit.

讯享网number ^= 1 << x;

That will toggle bit x.

3.Clearing a bit

Use the bitwise AND operator (&) to clear a bit.

number &= ~(1 << x);

That will clear bit x. You must invert the bit string with the bitwise NOT operator (~), then AND it.


讯享网

4.Checking a bit

To check a bit, shift the number x to the right, then bitwise AND it:

讯享网bit = (number >> x) & 1;

That will put the value of bit x into the variable bit

5.Changing the nth bit to x

二、C decimal 操作 (C语言 十进制位 操作)

1.Get a 位
和二进制的操作比较类似,其中的<<(二进制左移)在10进制里面相当于÷10。在10进制里面可以用循环来实现。

2.Set a 位

参考:


REFERENCE:How do you set, clear and toggle a single bit in C/C++?

小讯
上一篇 2025-03-04 23:56
下一篇 2025-01-04 16:54

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/13749.html