[英]Bitset program C++
我似乎在這里錯過了一些愚蠢的東西。
int main()
{
/*
bitset<sizeof(int)*8> bisetInt(64);
cout<<"Sizeof is "<<sizeof(int)<<endl;
for(int i=0;i< sizeof(int)*8;i++)
cout<<bisetInt[i]<<endl;
//cout<<"no bits set are "<<bisetInt.count()<<endl;
*/
int num = 5;
int x = 1;
for (int i = 0;i < 5; i++)
{
x = x<<i;
cout<< (num & x)<<endl;
cout<<"value of x is "<<x<<endl;
}
return 0;
}
輸出:
1
value of x is 1
0
value of x is 2
0
value of x is 8
0
value of x is 64
0
value of x is 1024
一會兒我以為是
x = x<<i; //not this
x<<i;//rather this
只是根本不改變x的值。 我不確定為什么x從2-> 8-> 64跳
干杯!
您每次在循環中都移動i
位。
第一次將0移位,保持1。然后將1移位,使1變成2。然后再移位2位,使其變成8,依此類推。 也許你是說
x = 1 << i;
它將打印一個整數值,並依次設置較大的單個位(1、2、4、8,...)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.