1. 程式人生 > >C語言中的位遮蔽(bit masking)

C語言中的位遮蔽(bit masking)

位遮蔽的含義是從包含多個位集的一個或一組位元組中選出指定的一(些)位。為了檢查一個位元組中的某些位,可以讓這個位元組和遮蔽字(bit mask)進行按位與操作(C的按位與運算子為&)——遮蔽字中與要檢查的位對應的位全部為1,而其餘的位(被遮蔽的位)全部為0。例如,為了檢查變數flags的最低位,你可以讓flags和最低位的遮蔽字進行按位與操作:
flags&1;
為了置位所需的位,可以讓資料和遮蔽字進行按位或操作(C的按位或運算子為|)。例如,你可以這樣置位flags的最低位:
flags = flags | 1;
或者這樣:
flags |= 1;
為了清除所需的位,可以讓資料和對遮蔽字按位取反所得的值進行按位與操作。例如,你可以這樣清除flags的最低位:
flags = flags& ~1;
或者這樣:
flags&=~1 ;
有時,用巨集來處理標誌會更方便,例10.2中的程式就是通過一些巨集簡化了位操作。
例10.2 能使標誌處理更方便的巨集
/*

Bit Masking * /
/ * Bit masking can be used to switch a character
between lowercase and uppercase * /
#define BIT_POS(N) ( 1U ??(N) )
#define SET_FLAG(N,F) ( (N) | = (F) )
#define CLR_FLAG(N,F) ( (N) &= - (F) )
#define TST_FLAGCN,F) ( (N) & (F) )
#define BIT_RANGE(N,M) ( BIT_POS((M) + 1- (N))-1<<(N))
#define BIT_SHIFTL(B,N) ( (unsigned)(B)??(N) )
#define BIT_SHIFTR(B,N) ( (unsigned)(B)??(N) )
#define SET_MFLAG(N,F,V) ( CLR_FLAG(N,F), SET_FLAG(N,V) )
#define CLR_MFLAG(N,F) ( (N) &= ~(F) )
#define GET_MFLAG(N,F) ( (N) & (F) )
# include <stdio. h>
void main()
{
unsigned char ascii_char = 'A'; /* char = 8 bits only */
int test_nbr = 10;
printf("Starting character = %c\n" , ascii_char);
/" The 5th bit position determines if the character is
uppercase or lowercase.
5th bit = 0 - Uppercase
5th bit = 1- Lowercase * /
printf ("\nTurn 5th bit on = %c\n" , SET_FLAG(ascii_char, BIT_POS(5)));
printf ("Turn 5th bit off = %c\n\n",CLR_FLAG(ascii_char, BIT_POS(5)));
printf ("Look at shifting bits\n");
printf (" = = = = = = = = = = = = = = = =\n" );
printf ("Current value = %d\n" , test_nbr)i
printf ("Shifting one position left = %d\n" ,
test_nbr = BIT_SHIFTL(test_nbr, 1) );
printf ("Shifting two positions right = %d\n" ,
BIT_SHIFTR(test_nbr, 2) );
}
巨集BIT_POS(N)能返回一個和N指定的位對應的遮蔽字(例如BIT_POS(O)和BIT_POS(1)分別返回最低位和倒數第二位的遮蔽字),因此你可以用
#define A_FLAG BIT_POS(12)
#define A_FLAG BIT_P0S(13)
代替
#define A_FLAG 4096
#define A_FLAG 8192
這樣可以降低出錯的可能性。
巨集SET_FLAG(N,F)能置位變數N中由值F指定的位,而巨集CLR_FLAG(N,F)則剛好相反,它能清除變數N中由值F指定的位。巨集TST_FLAG(N,F)可用來測試變數N中由值F指定的位,例如:
if (TST_FLAG (flags, A_FLAG))
/* do something * /;
巨集BIT_RANGE(N,M)能產生一個與由N和M指定的位之間的位對應的遮蔽字,因此,你可以用
# define FIRST_OCTAL_DIGIT BIT_RANGE (0,2) /*111"/
# define SECOND-OCTAL-DIGIT BIT-RANGE(3,5) /* 111000*/
代替
#define FIRST_OCTAL_DIGIT 7 /*111*/
#define SECOND_OCTAL_DIGIT 56 /* 111000 * /
這樣可以更清楚地表示所需的位。
巨集BIT_SHIFT(B,N)能將值B移位到適當的區域(從由N指定的位開始)。例如,如果你用標誌C表示5種可能的顏色,你可以這樣來定義這些顏色:
#define C_FLAG BIT-RANGE(8,10) /* 11100000000 */
/* here are all the values the C flag can take on * /
# define C_BLACK BIT-SHIFTL(0,8) /* ooooooooooo */
# define C-RED BIT_SHIFTL(1,8) /* 00100000000 */
# define C-GREEN BIT_SHIFTL(2,8) /* 01000000000 */
# define C-BLUE BIT-SHIFTL(3,8) /* 01100000000 */
# define C_WHITE BIT-SHIFTL(4,8) /* 10000000000 */
# defineC-ZERO C-BLACK
# defineC-LARGEST C-WHITE
/* A truly paranoid programmer might do this */
#if C_LARGEST > C_FLAG
Cause an error message. The flag C_FLAG is not
big enough to hold all its possible values.
#endif /* C_LARGEST > C_FLAG */
巨集 SET_MFLAG(N,F,V)先清除變數N中由值F指定的位,然後置位變數N中由值V指定的位。巨集CLR_MFLAG(N,F)的作用和 CLR_FLAG(N,F)是相同的,只不過換了名稱,從而使處理多位標誌的巨集名字風格保持一致。巨集GET_MFLAG(N,F)能提取變數N中標誌F的 值,因此可用來測試該值,例如:
if (GET_MFLAG(flags, C_FLAG) == C_BLUE)
/*do something */;
注意:巨集BIT_RANGE()和SET_MFLAG()對引數N都引用了兩次,因此語句
SET_MFLAG(*x++,C_FLAG,C_RED);
的行為是沒有定義的,並且很可能會導致災難性的後果。