bitset用法总结

一般而言,标准库提供的bitset操作更直接、更容易阅读和书写、正确使用的可能性更高。而且,bitset对象的大小不受unsigned数的位数限制。通常来说,bitset优于整形数据的低级位操作。

 

bitset的几个操作和位运算之间的转化。

bitset<32> bs(88);

unsigned long l=88;

bs.set(6);

l |= 1UL<<6;

bs.reset(6);

l &= ~(1UL<<6)

 

构造函数可以传入一个string或者unsigned long。

简单介绍bitset的方法,copy msdn(在这里不做翻译了)。

any

The member function tests whether any bit in the sequence is set to 1.

count

The member function returns the number of bits set in the bit sequence.

flip

Toggles the value of all the bits in a bitset or toggles a single bit at a specified position.

这个函数的作用就是将所有的bit求反或者将指定位的bit求反,注意位都是从右边0开始的。

none

Tests if no bit has been set to 1 in a bitset object.

reset

Resets all the bits in a bitset to 0 or resets a bit at a specified position to 0.

set

Sets all the bits in a bitset to 1 or sets a bit at a specified position to 1.

size

Returns the number of bits in a bitset object.

test

Tests whether the bit at a specified position in a bitset is set to 1.

to_string

Converts a bitset object to a string representation.

to_ulong

Converts a bitset object to the integer that would generate the sequence of bits contained if used to initialize the bitset .

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值