zoukankan      html  css  js  c++  java
  • 二进制&bitset(未完成)

    1.

    与(&)(and):1&1=1, 0&1=0 ,1&0=0 ,0&0=0相同为1,不同为0

    或(|)(or):1|1=1 ,0|1=1, 1|0=1 ,0|0=0  有1得1

    异或(⊕)(xor):1⊕1=0, 0⊕1=1 ,1⊕0=1 ,0⊕0=0相同为0,不同为1

    非(¬)(not): ¬ 1=0 ¬0=1

    2.用途:

    与(&)主要用于取出某个二进制位 

      读某些位 读x的第pos个二进制位 (x>>pos)&1

      读x的第pos位开始的cnt位 (x>>pos)&((1u<<cnt)-1)

    或(|)主要用于强制给某位赋值

    异或(⊕)主要用于取反,它的逆运算是其本身。

    非(¬)将01全部取反。(对于有符号类型,符号位也会被取反)

      将某些位置为1, 构造遮罩,对于要改为1的位,将遮罩的对应位设为1,否则为0 ;或一下这个遮罩

      将某些位置为0, 构造遮罩,对于要改为0的位,将遮罩的对应位设为0,否则为1 ;与一下这个遮罩

      将某些位置取反 ,构造遮罩,对于要取反的位,将遮罩的对应位设为1,否则为0 ;异或一下这个遮罩

    判断奇偶性:&1

    对二的幂取模:x&((1<<y)-1)

    交换两个数:a^=b^=a^=b

    比较两个数是否相等:xor

    3.

    逻辑(算术)移位用途: <<1 -> *2           <<2 -> *4        >>1 -> /2        >>2 ->/4

    4.

    5.

    把x加一以后,最右边连续的1会变为0 ,最右边的0变成1(NOI2017D1T1),其它位不变 x&(x^(x+1))

    6.位运算表示集合

    交集:与

    并集:或

    补集:非

    x&y的差集:删除掉x与y的交 x^(x&y)

    枚举子集:for(int i=x;i;i=i&(i-1))

    bitset

    C++的 bitset 在 bitset 头文件中,它是一种类似数组的结构,它的每一个元素只能是0或1,每个元素仅用1bit空间。

    bitset常用构造函数有四种,如下

        bitset<4> bitset1;  //无参构造,长度为4,默认每一位为0
    
        bitset<8> bitset2(12);  //长度为8,二进制保存,前面用0补充
    
        string s = "100101";
        bitset<10> bitset3(s);  //长度为10,前面用0补充
        
        char s2[] = "10101";
        bitset<13> bitset4(s2);  //长度为13,前面用0补充
    
        cout << bitset1 << endl;  //0000
        cout << bitset2 << endl;  //00001100
        cout << bitset3 << endl;  //0000100101
        cout << bitset4 << endl;  //0000000010101

    注意:

    用字符串构造时,字符串只能包含 '0' 或 '1' ,否则会抛出异常。

    构造时,需在<>中表明bitset 的大小(即size)。

    在进行有参构造时,若参数的二进制表示比bitset的size小,则在前面用0补充(如上面的栗子);若比bitsize大,参数为整数时取后面部分,参数为字符串时取前面部分(如下面栗子):

        bitset<2> bitset1(12);  //12的二进制为1100(长度为4),但bitset1的size=2,        只取后面部分,即00
        string s = "100101";  
        bitset<4> bitset2(s);  //s的size=6,而bitset的size=4,只取前面部分,即1001
    
        cout << bitset1 << endl;  //00
        cout << bitset2 << endl;  //1001

    bitset对于二进制有位操作符,具体如下

        bitset<4> foo (string("1001"));
        bitset<4> bar (string("0011"));
    
        cout << (foo^=bar) << endl;       // 1010 (foo对bar按位异或后赋值给foo)
        cout << (foo&=bar) << endl;       // 0010 (按位与后赋值给foo)
        cout << (foo|=bar) << endl;       // 0011 (按位或后赋值给foo)
    
        cout << (foo<<=2) << endl;        // 1100 (左移2位,低位补0,有自身赋值)
        cout << (foo>>=1) << endl;        // 0110 (右移1位,高位补0,有自身赋值)
    
        cout << (~bar) << endl;           // 1100 (按位取反)
        cout << (bar<<1) << endl;         // 0110 (左移,不赋值)
        cout << (bar>>1) << endl;         // 0001 (右移,不赋值)
    
        cout << (foo==bar) << endl;       // false (0110==0011为false)
        cout << (foo!=bar) << endl;       // true  (0110!=0011为true)
    
        cout << (foo&bar) << endl;        // 0010 (按位与,不赋值)
        cout << (foo|bar) << endl;        // 0111 (按位或,不赋值)
        cout << (foo^bar) << endl;        // 0101 (按位异或,不赋值)

    此外,可以通过 [ ] 访问元素(类似数组),注意最低位下标为0,如下:

        bitset<4> foo ("1011");
        cout << foo[0] << endl;  //1
        cout << foo[1] << endl;  //1
        cout << foo[2] << endl;  //0

    当然,通过这种方式对某一位元素赋值也是可以的,栗子就不放了。

    bitset还支持一些有意思的函数,比如:

        bitset<8> foo ("10011011");
    
        cout << foo.count() << endl;  //5  (count函数用来求bitset中1的位数,foo中共有5个1
        cout << foo.size() << endl;   //8  (size函数用来求bitset的大小,一共有8位
    
        cout << foo.test(0) << endl;  //true  (test函数用来查下标处的元素是0还是1,并返回false或true,此处foo[0]为1,返回true
        cout << foo.test(2) << endl;  //false  (同理,foo[2]为0,返回false
    
        cout << foo.any() << endl;  //true  (any函数检查bitset中是否有1
        cout << foo.none() << endl;  //false  (none函数检查bitset中是否没有1
        cout << foo.all() << endl;  //false  (all函数检查bitset中是全部为1

    补充说明一下:test函数会对下标越界作出检查,而通过 [ ] 访问元素却不会经过下标检查,所以,在两种方式通用的情况下,选择test函数更安全一些

    另外,含有一些函数:

        bitset<8> foo ("10011011");
    
        cout << foo.flip(2) << endl;  //10011111  (flip函数传参数时,用于将参数位取反,本行代码将foo下标2处"反转",即0变1,1变0
        cout << foo.flip() << endl;   //01100000  (flip函数不指定参数时,将bitset每一位全部取反
    
        cout << foo.set() << endl;    //11111111  (set函数不指定参数时,将bitset的每一位全部置为1
        cout << foo.set(3,0) << endl;  //11110111  (set函数指定两位参数时,将第一参数位的元素置为第二参数的值,本行对foo的操作相当于foo[3]=0
        cout << foo.set(3) << endl;    //11111111  (set函数只有一个参数时,将参数下标处置为1
    
        cout << foo.reset(4) << endl;  //11101111  (reset函数传一个参数时将参数下标处置为0
        cout << foo.reset() << endl;   //00000000  (reset函数不传参数时将bitset的每一位全部置为0

    同样,它们也都会检查下标是否越界,如果越界就会抛出异常

    最后,还有一些类型转换的函数,如下:(很不常用)

        bitset<8> foo ("10011011");
    
        string s = foo.to_string();  //将bitset转换成string类型
        unsigned long a = foo.to_ulong();  //将bitset转换成unsigned long类型
        unsigned long long b = foo.to_ullong();  //将bitset转换成unsigned long long类型
    
        cout << s << endl;  //10011011
        cout << a << endl;  //155
        cout << b << endl;  //155

    例1:给你n个数,其中有且仅有一个数出现了奇数次,其余的数出现了偶数次,让你输出这个出现了奇数次的数。

    要求线性时间,常数空间 (异或一下就可以了)//相同的都消了,不同的留下来了

    int a[11] = {0};
    int i;
    int n = 0;
    printf("please input the arr :");
    for(i = 0;i < 11;i++)
        scanf("%d",&a[i]);
    for(i = 0;i < 11;i++)
        n ^= a[i];
    printf("appear the one time number is %d",n);
    system("pause");
    return 0;

    例2:给你n个数,其中有且仅有两个数出现了奇数次,其余的数出现了偶数次,让你输出这两个出现了奇数次的数。 这题卡空间,同时只能存n+2个数。

    从头到尾异或一遍 这样我们就得到了需要求的两个数异或后的值 这两个数不相等,异或后的值(k)不为0。 我们随便找出来k二进制表示下为1的一位。 按照这位是0/1 分成两类 对每一类分别使用上一题的算法。

    #include <stdio.h>
    #include <stdlib.h>
    void findoddnum(int a[],int n);
    int main()
    {
        int a[12];
        int i;
        printf("please input the arr:");
        for(i = 0;i < 12;i++)
        {
            scanf("%d",&a[i]);
        }
        findoddnum(a,12);
        system("pause");
        return 0;
    }
    void findoddnum(int a[],int n)
    {
        int sum = 0;
        int i;
        int j;
        int c = 0;
        int b = 0;
        for(i = 0;i < n;i++)
        {
            sum ^=a[i];
        }
        if(sum == 0)
        {
            printf("No such number!");
        }
        else
        {
            int count = 0;
            int temp = sum;
            int temp1 = 0;
            while(!(temp&1))
            {
                temp >>=1;
                count++;
            }
            for(j = 0;j < n;j++)
            {
                temp1 = a[j];
                if((temp1>>count)&1)
                {
                    c ^= temp1;
                }
            }
            b = sum ^ c;
            printf("the first number is %d",c);
            printf("the second number is %d",b);
        }
    }

    例3:

    给你n个数,让你找两个数,使得两数 与 后结果最大。

    从最高位开始,如果有>=2个该位为1的数,则该位为0的数全部删除 否则跳过这一位

  • 相关阅读:
    SmartTimer——一种基于STM32的轻量级时钟调度器
    Python2/3中的urllib库
    Python 解析构建数据大杂烩 -- csv、xml、json、excel
    Python2/3 中执行外部命令(Linux)和程序(exe) -- 子进程模块 subprocess
    Python 中的命令行参数处理 -- argparse、optparse、getopt
    Linux 定时循环执行 python 脚本
    Python2/3的中、英文字符编码与解码输出: UnicodeDecodeError: 'ascii' codec can't decode/encode
    在windows下使用Qt5开发GTK3图形界面应用程序
    qt编译的基于xlib cairo的桌面程序
    debian安装dwm窗口管理器
  • 原文地址:https://www.cnblogs.com/liukx/p/12452218.html
Copyright © 2011-2022 走看看