位运算

hdu又疯了。。又……sign……

所以不如来看下困惑很久的位运算吧。

说到为什么会来看位运算……今天做(抄)敌兵布阵时,理论上肯定是找最短的研究啦~可是……最短的充斥着|啊<<啊>>啊什么的……闹哪样!

不过简单入了下位运算的门后发现还是挺简单的东西呢……位运算果然就是一装逼利器……不不不,很有用的很有用的……吧……

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;


void change_to_binary(int a)
{
     int mask=1<<31;
     for(int i=1;i<=32;i++)
     {
             putchar(((a&mask)==0)?'0':'1');
             a<<=1;
     }
     putchar('\n');
}     

void exchange(int a,int b)
{
     a=a^b;
    b=a^b;
    a=a^b;
    cout<<"exchange(a,b) "<<a<<" "<<b<<endl;
}

void negative1(int a)
{
     a=~a+1;
     cout<<"negative(a) "<<a<<endl;
}

void negative2(int a)
{
     a=~(a-1);
     cout<<"negative(a) "<<a<<endl;
}

void plus_one(int a)
{
     a=-(~a);
     cout<<"plus_one(a) "<<a<<endl;
}

void minus_one(int a)
{
     a=~(-a);
     cout<<"minus_one(a) "<<a<<endl;
}

void odd_even(int a)
{
     if(a&1==0)
     cout<<a<<" is even"<<endl;
     else
     cout<<a<<" is odd"<<endl;
}

void get_kth_bit(int a,int k)
{
     int res;
     res=a>>k&1;
     //&1:get rid of the number before kth
     cout<<"the "<<k<<"th bit of "<<a<<" is "<<res<<endl;
     
}

void clear_kth_bit(int a,int k)
{
     int res;
     res=a&~(1<<k);
     //1<<k: ..01000...000(have k 0 after 1)
     //~(1<<k) ...1011111...111(have k 1 after 0)
     //1001010101010....1...101010100
     //  random         k   random     &
     //1111111111111111101..111111111
     //1001010101010....0...101010100
     
     cout<<"after "<<k<<"th bit becomes 0, now "<<a<<" beomes "<<res<<endl;  
}

void set_kth_bit_to_one(int a,int k)
{
     int res;
     res=a|(1<<k);
     cout<<"after "<<k<<"th bit becomes 1, now "<<a<<" beomes "<<res<<endl;  
}

void circle_move_left_kth_time(int a,int k)
{
     int res;
     res=a<<k|a>>32-k;//sizeof(int)=32
     //res=a<<k|a>>32-k;  //if you want to move right
     cout<<"after circle move "<<k<<"th times now "<<a<<" beomes "<<res<<endl;  
}

void average(int a,int b)
{
     int res;
     res=(a&b)+((a^b)>>1);
     cout<<"the average number of "<<a<<" and "<<b<<" is "<<res<<endl;
}

int main()
{
    int a=3;//11
    int b=4;//100
    int k=0;
    cout<<"original "<<a<<" "<<b<<endl;
    cout<<"sizeof(int) in bit "<<sizeof(int)*8<<endl;
    change_to_binary(a);
    exchange(a,b);
    negative1(a);
    negative2(a);
    plus_one(a);
    minus_one(a);
    odd_even(a);
    get_kth_bit(a,k);//k is count from 0
    clear_kth_bit(a,k);
    set_kth_bit_to_one(b,k);
    circle_move_left_kth_time(b,32);
    average(a,b);//this way won't overflow
    system("pause");
    return 0;
}

只是一部分,明天(或者什么时候hdu又疯了时)再继续吧……

现在的状态……算是神经麻痹么……

恩……更新……重贴一次吧。

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;


void change_to_binary(int a)
{
     int mask=1<<31;
     for(int i=1;i<=32;i++)
     {
             putchar(((a&mask)==0)?'0':'1');
             a<<=1;
     }
     putchar('\n');
}     

void exchange(int a,int b)
{
     a=a^b;
    b=a^b;
    a=a^b;
    cout<<"exchange(a,b) "<<a<<" "<<b<<endl;
}

void negative1(int a)
{
     a=~a+1;
     cout<<"negative(a) "<<a<<endl;
}

void negative2(int a)
{
     a=~(a-1);
     cout<<"negative(a) "<<a<<endl;
}

void plus_one(int a)
{
     a=-(~a);
     cout<<"plus_one(a) "<<a<<endl;
}

void minus_one(int a)
{
     a=~(-a);
     cout<<"minus_one(a) "<<a<<endl;
}

void odd_even(int a)
{
     if(a&1==0)
     cout<<a<<" is even"<<endl;
     else
     cout<<a<<" is odd"<<endl;
}

void get_kth_bit(int a,int k)
{
     int res;
     res=a>>k&1;
     //&1:get rid of the number before kth
     cout<<"the "<<k<<"th bit of "<<a<<" is "<<res<<endl;
     
}

void clear_kth_bit(int a,int k)
{
     int res;
     res=a&~(1<<k);
     //same to: res=a|1-1;
     //1<<k: ..01000...000(have k 0 after 1)
     //~(1<<k) ...1011111...111(have k 1 after 0)
     //1001010101010....1...101010100
     //  random         k   random     &
     //1111111111111111101..111111111
     //1001010101010....0...101010100
     
     cout<<"after "<<k<<"th bit becomes 0, now "<<a<<" beomes "<<res<<endl;  
}

void set_kth_bit_to_one(int a,int k)
{
     int res;
     res=a|(1<<k);
     cout<<"after "<<k<<"th bit becomes 1, now "<<a<<" beomes "<<res<<endl;  
}

void circle_move_left_kth_time(int a,int k)
{
     int res;
     res=a<<k|a>>32-k;//sizeof(int)=32
     //res=a<<k|a>>32-k;  //if you want to move right
     cout<<"after circle move "<<k<<"th times now "<<a<<" beomes "<<res<<endl;  
}

void average(int a,int b)
{
     int res;
     res=(a&b)+((a^b)>>1);
     cout<<"the average number of "<<a<<" and "<<b<<" is "<<res<<endl;
}

void check_whether_power_of_two(int a)
{
     bool res;
     res=a&(a-1);
     //100
     //   &
     // 11
     //000
     cout<<a<<" is ";
     if((res!=0)||(a==0))
         cout<<"not ";
     cout<<"power of two"<<endl;
}

void bitcount(int a)
{
     int value=0;
     int save=a;
     while(a)
     {
             value++;
             a&=a-1;
     }
     cout<<save<<" has "<<value<<" 's one in it"<<endl;
}

void special_assign(int x,int a,int b)
{
    x=a^b^x;
    //if(x==a)x=b;
    //else x=a;
    cout<<x<<endl;
}
     
void number_of_one_is_evenodd(int a)
{
     int res=a;
     res^=(res>>1);
     res^=(res>>2);
     res^=(res>>4);
     res^=(res>>8);
     res^=(res>>16);
     cout<<"the number of one in "<<a<<" is ";
     if((res&1)==1)
     cout<<"odd"<<endl;
     else
     cout<<"even"<<endl;
}

void bitcount2(int a)
{
     int x=a;
     x = (x&0x55555555) + ((x>>1)&0x55555555); 
     x = (x&0x33333333) + ((x>>2)&0x33333333); //00110011001100
     x = (x&0x0F0F0F0F) + ((x>>4)&0x0F0F0F0F); 
     x = (x&0x00FF00FF) + ((x>>8)&0x00FF00FF); 
     x = (x&0x0000FFFF) + ((x>>16)&0x0000FFFF);
     cout<<a<<" has "<<x<<" 's one in it"<<endl;
} 

void leading_zeros_number(unsigned x)//here!must be unsigned!!!important!
{
     int res=1;
     int save=x;
     if(x==0) res=32;
     else
     {
     if((x>>16)==0)
     {res+=16;x<<=16;}
     if((x>>24)==0)
     {res+=8;x<<=8;}
     if((x>>28)==0)
     {res+=4;x<<=4;}
     if((x>>30)==0)
     {res+=2;x<<=2;}
     res=res-(x>>31);
     }
     cout<<"there are "<<res<<" leading zeros before "<<save<<endl;
}
  
void  binary_in_reverse_order(int a)
{
      
     int  x=a;
     x = (x&0x55555555) <<  1 | (x&0xAAAAAAAA) >>  1;
   x = (x&0x33333333) <<  2 | (x&0xCCCCCCCC) >>  2;
   x = (x&0x0F0F0F0F) <<  4 | (x&0xF0F0F0F0) >>  4;
   x = (x&0x00FF00FF) <<  8 | (x&0xFF00FF00) >>  8;
   x = (x&0x0000FFFF) << 16 | (x&0xFFFF0000) >> 16;
   cout<<x<<endl;
}
  
  
void special_exchange(int a)
{
    int res;
    res=(a>>16)|(a<<16);
    cout<<res<<endl;
    //数1314520用二进制表示为0000 0000 0001 0100 0000 1110 1101 1000(添加了11个前导0补足为32位),其中前16位为高位,即0000 0000 0001 0100;后16位为低位,即0000 1110 1101 1000。将它的高低位进行交换,我们得到了一个新的二进制数0000 1110 1101 1000 0000 0000 0001 0100。它即是十进制的249036820。
}
     
void code()
{
    long y;
    y=727589^6783;
    cout<<y<<" ";
    y^=6783;
    cout<<y<<endl;
}

int main()
{
    int a=3;//11
    int b=4;//100
    int k=0;
    cout<<"original "<<a<<" "<<b<<endl;
    cout<<"sizeof(int) in bit "<<sizeof(int)*8<<endl;
    cout<<"something easy:"<<endl;
    cout<<"a*(2^n)==a<<n"<<endl;
    cout<<"a/(2^n)==a>>n"<<endl;
    cout<<"a%2==a&1"<<endl;
    cout<<"upgrade version: a%(2^n)==a&(2^n-1)"<<endl;
    change_to_binary(a);
    exchange(a,b);
    negative1(a);
    negative2(a);
    plus_one(a);
    minus_one(a);
    odd_even(a);
    get_kth_bit(a,k);//k is count from 0
    clear_kth_bit(a,k);
    set_kth_bit_to_one(b,k);
    circle_move_left_kth_time(b,32);
    average(a,b);//this way won'ht overflow
    check_whether_power_of_two(a);
    bitcount(a);
    number_of_one_is_evenodd(a);
    bitcount2(a);
    leading_zeros_number(a);
    special_exchange(1314520);
    binary_in_reverse_order(1314520);
    //special_assign(1,a,b);
    code();
    system("pause");
    return 0;
}

注释掉的那个失败了,code的那个只是个无聊的小游戏而已,可忽视。

最近开始看一些奇怪的东西了……整个人都坏掉了……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值