如何理解程序

时间:2015-06-06 07:49:29

标签: c linux

int mask = 0700, N_BITS = 3;
struct stat buff;
static char perm[] = {"---", "--x", "-w-", "-w x", "r--", "r-x", "rw-", "rwx"};
stat(f_d, &buff); //get the permissions info of file
printf("%3s", perm[buff.st_mode & mask >> (i - 1) * N_BITS)] //HOW TO UNDERSTAND ?`
mask >>= N_BITS; //HOW TO UNDERSTAND ?

2 个答案:

答案 0 :(得分:1)

>>是按位右移运算符。

X >>= N

相当于

X = X >> N

一些例子:

  • (4>> 1)=(0100b>> 1)= 0010b = 2
  • (12>> 2)=(1100b>> 2)= 0011b = 3

答案 1 :(得分:1)

>>bitwise right shift operator

mask>>=N_BITS相当于mask = mask >> N_BITS

所以最初

mask = 0b111000000;

N_BITS操作后mask>>=N_BITS等于 3

 mask = 0b000111000;