KNOW: Bit Manipulation

本文详细介绍了位运算符的使用方法及技巧,包括按位取反、左移右移、按位与、按位异或、按位或等操作,并提供了实际应用场景示例,如设置、清除、更新特定位的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bit Operators

Bitwise Operators
~bitwise NOT~expr
<<, >>left/right shiftexpr1 << expr2
&bitwise ANDexpr1 & expr2
^bitwise XORexpr1 ^ expr2
|bitwise ORexpr1 | expr2

1. listed by their precedence

2. bitwise operators take operands of integral type

3. the sign bit is handled machine-dependent, so we'd better use unsigned type when using integral value with the bitwise operators

NOT operator (~) is similar in behavior to the bitset flip operation:

    unsigned char bits = 0227;     //10010111  
    bits = ~ bits;                 //01101000

Shift operator(<<, >>) will discard the bits that are shifted off the end. (The right-hand operand must not be negative and must be a value that is strictly less than the number of bits in the left-hand operand)

unsigned char bits = 0233;     //10011011  
    bits <<1;                      //00110110    the left most bit is discarded

And operator(&) will return 1 when both operands are 1; or 0.
XOR operator(^) will return 1 if either but not both operands contain 1 ; or 0.
OR operator(|) will return 1 if either or both operands are 1; or 0.
Bitset library are more direct easier than manipulate integral with bitwise operation.

Shift Operators has midlevel precedence:lower than arithmetic operators, but higher than relational, assignment, or conditional operators:

cout << 42 + 10;       //ok, + has higher precedence  
cout << (10 < 42);     //ok, parentheses force intended grouping, result is 1  
cout << 10 < 42;       //error, attempt to compare cout to 42  

Mask

wiki

In computer science, a mask is data that is used for bitwise operations, particularly in a bit field.

Using a mask, multiple bits in a byte, nibble, word (etc.) can be set either on, off or inverted from on to off (or vice versa) in a single bitwise operation.

Common Masking functions

Masking bit to 1

Y | 1 = 1

Masking bit to 0

Y & 0 = 0

Query Status of a bit

Turn off all other bits:

    10011101   10010101
AND 00001000   00001000
  = 00001000   00000000

Toggling bit values

Y ^ 0 = Y
Y ^ 1 = ~Y
// Example:
    10011101   10010101
XOR 00001111   11111111
  = 10010010   01101010

Bit Facts and Tricks

x ^ 0s = xx & 0s = 0x | 0s = x
x ^ 1s = ~xx & 1s = xx | 1s = 1s
x ^ x = 0x & x = xx | x = x

Common Bit Tasks

Get Bit

Get certain bit in a int value num:

boolean getBit(int num, int i)
{
    return ( (num & (1 << i))!=0 );
}

Set Bit

set certain bit:

int setBit(int num, int i)
{
    return num | (1 << i);
}

Clear Bit

clear ith bit:

int clearBit(int num, int i)
{
    int mask = ~(1 << i);
    return num & mask;
}
clear all bits from most significant bit through i(inclusive)
int clearBitsMSBthroughI(int num, int i)
{
    int mask = (1 << i) - 1;
    return num & mask;
}
clear all bits from i to 0(inclusive)

int clearBitsIthrough0(int num, int i)
{
    int mask = ~((1 << (i+1)) - 1);
    return num & mask;
}

Update Bit

// update the ith bit to value v (v is 0 or 1)
int updateBit (int num, int i, int v)
{
    int mask = ~(1 << i);
    return (num & mask) | (v << i);
}

Decimal to Binary translate

http://www.wikihow.com/Convert-from-Decimal-to-Binary

http://cs.furman.edu/digitaldomain/more/ch6/dec_frac_to_bin.htm


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值