我有一個程序,我的教授給我一個硬件,我想看看你們是否可以解釋我是如何工作的。注意:我不想讓你們給我答案;我想學習,所以如果你們能向我解釋這項工作是如何令人敬畏的,那麼我可以在我的主持下開始工作。在C程序中對位掩碼的理解
說明:
一個)unsigned setbits (unsigned x, int p, int n, unsigned y)
其返回x與開始位置p處的n位(右調整)設置爲y的最右邊的n位,而使其他位不變。注意:雖然它不會更改x和y的值。
b)unsigned invertbits (unsigned x, int p, int n)
返回x,其中在位置p(右調整)開始的n位反轉,即1變爲0,反之亦然,其餘位保持不變。注意:它不會改變x的值。
#include <stdio.h>
#include <limits.h>
void bit_print(int);
int pack(char, char, char, char);
char unpack(int, int);
unsigned getbits(unsigned, int, int);
void bit_print(int a){
int i;
int n = sizeof(int) * CHAR_BIT;
int mask = 1 << (n-1); // mask = 100...0
for (i=1; i<=n; i++){
putchar(((a & mask) == 0)? '0' : '1');
a <<= 1;
if (i % CHAR_BIT == 0 && i < n)
putchar(' ');
}
putchar('\n');
}
int pack(char a, char b, char c, char d){
int p=a;
p = (p << CHAR_BIT) | b;
p = (p << CHAR_BIT) | c;
p = (p << CHAR_BIT) | d;
return p;
}
char unpack(int p, int k){ // k=0, 1, 2, or 3
int n = k * CHAR_BIT; // n = 0, 8, 16, 24
unsigned mask = 255; // mask = low-order byte
mask <<= n;
return ((p & mask) >> n);
}
// getbits() extracts n bits from position p(start counting from the right-most bit) in x
unsigned getbits(unsigned x, int p, int n){
unsigned temp = x >> (p+1-n);
unsigned mask = 0;
mask = ~mask;
mask = mask << n;
mask = ~mask;
return temp & mask;
// return (x >> (p+1-n)) & ~(~0<<n);
}
int main(){
int x = 19;
printf("The binary rep. of %d is:\n", x);
bit_print(x);
int p=pack('w', 'x', 'y', 'z');
printf("\n'w', 'x', 'y', and 'z' packed together is equal to %d. Its binary rep. is:\n", p);
bit_print(p);
printf("calling unpack(p, 0) to extract the byte # 0 from the right:\n");
bit_print(unpack(p, 0));
printf("calling unpack(p, 1) to extract the byte # 1 from the right:\n");
bit_print(unpack(p, 1));
printf("calling unpack(p, 2) to extract the byte # 2 from the right:\n");
bit_print(unpack(p, 2));
printf("calling unpack(p, 3) to extract the byte # 3 from the right:\n");
bit_print(unpack(p, 3));
unsigned result = getbits(p, 20, 7);
printf("\ncalling getbits(p, 20, 7) to extract 7 bits from bit # 20 returns %d:\n", result);
bit_print(result);
return 0;
}
您有具體問題嗎?如何工作是非常廣泛的,它看起來像它會在你的課程涵蓋之前,這項作業被分配。 – 2014-09-03 16:23:36
@RetiredNinja他們給了我們一個介紹,但我不明白 – fuscode 2014-09-03 16:42:18