套路:
1-清零
2-相应位置1
3-查找数相对应的位为1,就输出;
</pre><p><span style="font-size:18px"></span></p><pre code_snippet_id="447379" snippet_file_name="blog_20140811_1_5432974" name="code" class="cpp">#include<stdio.h>
int set_bit(int *bit ,unsigned n)
{
bit[n/32] |= 1<<(n%32);//一般不推荐用* /和%,占用CPU周期比较多,建议用 >> 和 << ,n/32 == n>>5 , n%32 ==n&0x1f
return 2 ;
}
int main()
{
int i , index = 0,n ,b[10]={0};
unsigned a[300];
for (i = 0 ; i< 300 ; i++ )
a[i] = i;
for (i = 0 ; i < 300 ; i++ )
set_bit( b , a[i] );
for (i = 0 ; i< 300 ; i++ )
{
if ( b[index] & (1<<(i&0x1f)) ) //相对应的数如果存在位为1,就输出
{
printf("%d \t", i ) ;
}
if(i%32 == 31)
{
index++; //控制下标的换 , 0-31 32-63 64-95 ---
}
}
return 3;
}
#endif</span>