當前位置:首頁 » 編程語言 » c語言mask

c語言mask

發布時間: 2023-09-04 12:51:21

Ⅰ 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 �0�0(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)�0�0(N) )
#define BIT_SHIFTR(B,N) ( (unsigned)(B)�0�3(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);
的行為是沒有定義的,並且很可能會導致災難性的後果。

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:433
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:744
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:537
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:147
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:240
java駝峰 發布:2025-02-02 09:13:26 瀏覽:652
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:538
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726