C / C ++ Title - Bitwise

Title: There is such a clever expression:

a^=b^=a^=b;

It does not require temporary variables can exchange the values ​​of a and b.

[ Answer ]

This is not portable. It attempts to modify variables twice a sequence of points in between, and this behavior is undefined. For example, it was reported the following code:

int a=123,b=7654;

a^=b^=a^=b;

In the SCO optimizing C compiler will set to 123 b, the a is set to 0.

Title: takes an integer from 4 to 7 from the right end of a.

【answer】

main()
{
unsigned a,b,c,d;
scanf("%o",&a);
b=a>>4;
c=~(~0<<4);
d=b&c;
printf("%o\n%o\n",a,d);

}

Title: x a statement implemented to judge whether a power of 2.

【answer】

void mai n()                                               
{                                         
i nt a;                                                        
scanf(“%d”,&a);                                   
printf(“%c”,(a)&(a-1)?’n’:’y’); //   若是打印y,否则n         
}

Topic: Please return the following value (Microsoft) function

int fun(x)
{
int countx=0;
while(x)
{
countx++;
x=x&(x-1);
}
return countx;
}

Assuming x = 9999.

【answer】

8

 

 

 

Guess you like

Origin blog.csdn.net/chen1083376511/article/details/92001876