当前位置:网站首页>bitset的基本用法
bitset的基本用法
2022-08-04 10:47:00 【beyond+myself】
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<bitset>
using namespace std;
int main()
{
//bitset的长度在设置的时候应该请确定
//可以单独访问某个元素例如bit[0]表示第一个元素
bitset<4> bit1;
cout<<bit1<<endl;//默认全为0
bitset<4> bit2(5);//设置初始值
cout<<bit2<<endl;
bitset<4> bit3(17);
cout<<bit3<<endl;//如果初始值大于位数,则高位舍弃,只保留低位
bitset<4> bit4(string("1111"));//用字符串初始化
cout<<bit4<<endl;
cout<<(bit2&bit3)<<endl;//注意加上括号
cout<<(bit2|bit3)<<endl;
cout<<(~bit1)<<endl;
cout<<(bit1^bit2)<<endl;
cout<<(bit1<<1)<<endl;
cout<<(bit2>>1)<<endl;
cout<<bit2.size()<<endl;//返回大小
cout<<bit2.count()<<endl;//返回1的数量
cout<<bit2.any()<<endl;//返回是否含有1
cout<<bit2.none()<<endl;//返回是否没有1
cout<<bit2.set()<<endl;//将所有位数设置为1
cout<<bit1.set(0)<<endl;//将第p+1为设置为1
cout<<bit1.set(0,0)<<endl;//将第p+1为设置为0
cout<<bit1.reset()<<endl;//全部变为0
cout<<bit1.reset(0)<<endl;//将第p+1为设置为0
cout<<bit1.flip(0)<<endl;//将第p+1位取反
cout<<bit1.to_string()<<endl;//将其转化位字符串输出
cout<<bit1.to_ulong()<<endl;//转化为unsinged long输出,超出范围则报错
cout<<bit1.to_ullong()<<endl;//转化为unsigned long long,超出范围则报错
cout<<bit1[0]<<endl;
printf("%s\n",bit1);//默认是字符串类型的
return 0;
}
边栏推荐
- gom登录器配置教程_谷歌浏览器如何使用谷歌搜索引擎
- 华为开源:聚焦开源基础软件,共建健康繁荣生态
- What is the principle of thermal imaging temperature measurement?Do you know?
- 2万字50张图玩转Flink面试体系
- 无代码平台多项选择入门教程
- Mobile open source low code tools beeware and kivy
- Win11 file types, how to change?Win11 modify the file suffix
- RAID介绍及RAID5配置实例
- SkiaSharp 之 WPF 自绘 粒子花园(案例版)
- 物体颜色的来源
猜你喜欢

《迁移学习导论》第2版,升级内容抢先看!

数据化管理洞悉零售及电子商务运营——零售密码

zabbix deployment

C language * Xiaobai's adventure

Multimedia and Internet of Things technology make the version "live" 129 vinyl records "Centennial Voice"

图文手把手教程--ESP32 OTA空中升级(VSCODE+IDF)

MySQL之my.cnf配置文件

STM32前言知识总结

audio_policy_configuration.xml配置文件详解

热成像测温的原理是什么呢?你知道吗?
随机推荐
nsq部署_andlua辅助源码
Apache Calcite 框架原理入门和生产应用
第二批养老理财试点产品发行 一小时销售20亿元
cubemx stm32 afm3000 module gas flow sensor driver code
8月活动|51CTO十七周年庆,发博文得茶具/笔记本/T恤等礼品!
Heap Sort
双向带头循环链表实现
MySQL:完整性约束和 表的设计原则
学会使用set和map的基本接口
Graphical Hands-on Tutorial--ESP32 One-Key Network Configuration (Smartconfig, Airkiss)
ORB-SLAM3中的优化
昨夜梦佳人,七夕试伊妆丨基于ModelArts实现AI妆容迁移丨【玩转华为云】
无代码平台数字入门教程
XCTF-reverse-signin
Camunda整体架构和相关概念
matlab练习程序(多线段交点)
[代码阅读] CycleGAN: Unpaired Image-To-Image Translation Using Cycle-Consistent Adversarial Networks
Maple 2022软件安装包下载及安装教程
JS工厂模式_工厂模式进行封装
audio_policy_configuration.xml配置文件详解