將 X 變數的第 n 個位元設定為 1 (bit從0開始數):
X = X | (1 << n)
Example_1:
X = 11110010,假設我們要把第2個bit改為1
==> X = (11110010) | (1 << 2)
==> X = (11110010) | (00000100)
==> X = 11110110
Example_2:
X = 11110010,假設我們要把第0個bit改為1
==> X = (11110010) | (1 << 0)
==> X = (11110010) | (00000001)
==> X = 11110011
將 X 變數的第 n 個位元設定為 0 (bit從0開始數):
X = X & ~(1 << n)
Example:
X = 11110010,假設我們要把第1個bit改為0
==> X = (11110010) & ~(1 << 1)
==> X = (11110010) & ~(00000010)
==> X = (11110010) & (11111101)
==> X = 11110000
沒有留言:
張貼留言