C++ Bitset 中的 test()函数

首页 / C++入门教程 / C++ Bitset 中的 test()函数

C++ bitset test()函数用于测试位置p处的位是否已设置。如果位设置在位置p,则返回true或false。

test - 语法

bool test(int p);

test - 参数

p :指定是否设置该位的索引。

test - 返回值

如果设置了位置p的位,则返回true;否则,返回false。

test - 例子1

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
    bitset<8> b(string("01010110"));
    bool a=b.test(3);
    bool c=b.test(2);
     cout << " bitset b is set at position 3 : " << a <<'\n';
     cout << " bitset b is set at position 2 : " << c <<'\n';
   return 0;
}

输出:

bitset b is set at position 3 : 0
 bitset b is set at position 2 : 1

test - 例子2

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
    bitset<8> b(string("01010110"));
    bitset<4> b1;
     cout << " bitset b is set at position 3 : " << b.test(3) <<'\n';
     cout << " bitset b1 is set at position 2 : " << b.test(2) <<'\n';
   return 0;
}

输出:

bitset b is set at position 3 : 0
bitset b1 is set at position 2 : 1

test - 例子3

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
    bitset<8> b(string("01010110"));
    if(b.test(1)){
     cout << "bitset is set at postion 1";
    }
    else
    {
        cout << "bitset is not set at position 1";
    }
   return 0;
}

输出:

bitset is set at postion 1

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入浅出gRPC -〔李林锋〕

Android开发高手课 -〔张绍文〕

深入拆解Tomcat & Jetty -〔李号双〕

雷蓓蓓的项目管理实战课 -〔雷蓓蓓〕

说透敏捷 -〔宋宁〕

Serverless入门课 -〔蒲松洋(秦粤)〕

讲好故事 -〔涵柏〕

超级访谈:对话汤峥嵘 -〔汤峥嵘〕

手把手带你写一个MiniSpring -〔郭屹〕

好记忆不如烂笔头。留下您的足迹吧 :)