ACM 位運算

判斷一個整數是否是 2 的冪 , 對於一個數 x >= 0 ,判斷他是否是 2 的冪  
boolean power2(int x)  {      return((x&(x-1))==0)&&(x!=0);  }  


 

 

For example:ios

#include<stdio.h> int main() {     printf(" *******int a=2; int b=3**********\n;");     int a=2;     int b=3;     printf("計算a&b: %d\n",a&b);     printf("計算a^b: %d\n",a^b);     printf("計算~a: %d\n",~a);     printf("計算a<<b: %d\n",a<<b);     printf("計算a>>b: %d\n",a>>b);     printf("計算a|b: %d\n",a|b); } 

求m*2^nide

#include<iostream> #include<string.h> #include<stdio.h> #include<ctype.h> #include<algorithm> #include<stack> #include<queue> #include<math.h> using namespace std; int f(int m,int n) {     return m<<n; } int main() {     int m,n;     while(scanf("%d%d",&m,&n)!=EOF)     {         int w=f(m,n);         printf("%d\n",w);     }     return 0; } 
相關文章
相關標籤/搜索