/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:
*
* "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
*
*
* "The authors and publisher have taken care in the preparation of this book,
* but make no expressed or implied warranty of any kind and assume no
* responsibility for errors or omissions. No liability is assumed for
* incidental or consequential damages in connection with or arising out of the
* use of the information or programs contained herein."
*
* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. Send your request for permission, stating clearly
* what code you would like to use, and in what specific way, to the following
* address:
*
* Pearson Education, Inc.
* Rights and Permissions Department
* One Lake Street
* Upper Saddle River, NJ 07458
* Fax: (201) 236-3290
*/
#include <cstddef>
#include <iostream>
#include <string>
using std::cout; using std::cin;
using std::endl;
using std::string;
using std::size_t;
#include <bitset>
using std::bitset;
int main()
{
bitset<32> bitvec(1ULL); // 32 bits; low-order bit is 1,
// remaining bits are 0
bool is_set = bitvec.any(); // true, one bit is set
bool is_not_set = bitvec.none(); // false, one bit is set
bool all_set = bitvec.all(); // false, only one bit is set
size_t onBits = bitvec.count(); // returns 1
size_t sz = bitvec.size(); // returns 32
bitvec.flip(); // reverses the value of all the bits in bitvec
bitvec.reset(); // sets all the bits to 0
bitvec.set(); // sets all the bits to 1
cout << "bitvec: " << bitvec << endl;
sz = bitvec.size(); // returns 32
onBits = bitvec.count(); // returns 1,
// i.e., the number of bits that are on
// assign 1 to even numbered bits
for (int index = 0; index != 32; index += 2)
bitvec[index] = 1;
// equivalent loop using set operation
for (int index = 0; index != 32; index += 2)
bitvec.set(index);
// bitvec is unchanged
bitset<32> b2 = ~bitvec; // b2 is a copy of bitvec with every bit flipped
// assign value of last bit in bitvec to the first bit in b2
b2[0] = bitvec[bitvec.size() - 1];
bitvec[0] = 0; // turn off the bit at position 0
bitvec[31] = bitvec[0]; // give last bit the same value as the first
bitvec[0].flip(); // flip the value of the bit at position 0
~bitvec[0]; // equivalent; flips the bit at position 0
bool b = bitvec[0]; // convert the value of bitvec[0] to bool
b2[0] = ~bitvec[0]; // first bit in b2 has the opposite value
// of the first bit in bitvec
unsigned i = 0;
if (bitvec.test(i))
// bitvec[i] is on
;
//equivalent test using subscript
if (bitvec[i])
// bitvec[i] is on
;
cout << "bitvec: positions turned on:\n\t";
for (int index = 0; index != 32; ++index)
if (bitvec[index])
cout << index << " ";
cout << endl;
// equivalent; turn off first bit
bitvec.flip(0); // reverses the value of the first bit
bitvec.set(bitvec.size() - 1); // turns on the last bit
bitvec.set(0, 0); // turns off the first bit
bitvec.reset(i); // turns off the ith bit
bitvec.test(0); // returns false because the first bit is off
bitvec[0] = 0;
bitvec.flip(0); // reverses value of first bit
bitvec[0].flip(); // also reverses the first bit
cout << "new inits" <<endl;
// bits13 is smaller than the initializer;
// high-order bits from the initializer are discarded
bitset<13> bits13(0xbeef); // bits are 1111011101111
// bits20 is larger than the initializer;
// high-order bits in bits20 are set to zero
bitset<20> bits20(0xbeef); // bits are 00001011111011101111
// on machines with 64-bit long long 0ULL is 64 bits of 0,
// so ~0ULL is 64 ones, so 0 ... 63 are one, and 64 ... 127 are zero
// if long long has 32 bits, 0 ... 32 are one, 33 ... 127 are zero
bitset<128> bits128(~0ULL);
cout << "bits13: " << bits13 << endl;
cout << "bits20: " << bits20 << endl;
cout << "bits128: " << bits128 << endl;
cout << "bits20[0] " << bits20[0] << endl;
cout << "bits20[19] " << bits20[19] << endl;
// bitvec1 is smaller than the initializer
bitset<32> bitvec1(0xffff); // bits 0 ... 15 are set to 1;
// 16 ... 31 are 0
// bitvec2 same size as initializer
bitset<64> bitvec2(0xffff); // bits 0 ... 15 are set to 1;
// 16 ... 63 are 0
// assuming 64-bit long long, bits 0 to 63 initialized from 0xffff
bitset<128> bitvec3(0xffff); // bits 32 through 127 are zero
cout << "bitvec1: " << bitvec1 << endl;
cout << "bitvec2: " << bitvec2 << endl;
cout << "bitvec2[0] " << bitvec2[0] << endl;
cout << "bitvec2[31] " << bitvec2[31] << endl;
cout << "bitvec3: " << bitvec3 << endl;
// pre C++ 11 has a constructor from string but not from const char*
bitset<32> bitvec4(string("1100")); // bits 2 and 3 are 1, all others are 0
cout << "strval: " << "1100" << endl;
cout << "bitvec4: " << bitvec4 << endl;
string str(string("1111111000000011001101"));
bitset<32> bitvec5(str, 5, 4); // four bits starting at str[5], 1100
bitset<32> bitvec6(str, str.size()-4); // use last four characters
cout << "str: " << str << endl;
cout << "bitvec5: " << bitvec5 << endl;
cout << "str: " << str << endl;
cout << "bitvec6: " << bitvec6 << endl;
unsigned long ulong = bitvec3.to_ulong();
cout << "ulong = " << ulong << endl;
bitset<32> bitvec7 = bitvec1 & bitvec4;
cout << "bitvec7: " << bitvec7 << endl;
bitset<32> bitvec8 = bitvec1 | bitvec4;
cout << "bitvec8: " << bitvec8 << endl;
bitset<16> chk(string("111100110011001100000"));
cout << "chk: " << chk << endl;
bitset<16> bits;
cin >> bits; // read up to 16 1 or 0 characters from cin
cout << "bits: " << bits << endl; // print what we just read
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
C++primer5的code (496个子文件)
absInt 65B
absInt 65B
accum 70B
add 6B
add 6B
add_item 144B
add_item 46B
add_item 46B
add_item 46B
add_item 46B
add_item 46B
add_item_data 190B
Alice 7B
Alice 7B
alloc 19B
badgetc 13B
runpgms.bat 629B
runpgms.bat 568B
runpgms.bat 529B
runpgms.bat 350B
runpgms.bat 334B
runpgms.bat 325B
runpgms.bat 288B
runpgms.bat 278B
runpgms.bat 239B
runpgms.bat 231B
runpgms.bat 225B
runpgms.bat 210B
runpgms.bat 197B
runpgms.bat 195B
runpgms.bat 186B
runpgms.bat 184B
runpgms.bat 168B
runpgms.bat 160B
runpgms.bat 137B
runpgms.bat 76B
runp.bat 28B
bits 10B
book_sales 234B
book_sales 234B
book_sales 234B
book_sales 234B
book_sales 184B
book_trans 46B
copyOut 4KB
count-size 50B
count-size 50B
bits.cpp 6KB
TextQuery.cpp 5KB
StrFolder.cpp 5KB
Folder.cpp 5KB
multidim.cpp 5KB
char_repl.cpp 5KB
newcount-size.cpp 5KB
findbook.cpp 5KB
newcount-size.cpp 5KB
word_transform.cpp 4KB
calc.cpp 4KB
ifgrades.cpp 4KB
Query.cpp 4KB
errMsg_initList.cpp 4KB
Basket_main.cpp 4KB
Sales_data.cpp 4KB
refparms.cpp 4KB
Array.cpp 4KB
Sales_data.cpp 4KB
sstream.cpp 4KB
str_assignOps.cpp 4KB
pair.cpp 4KB
oknew.cpp 4KB
calc.cpp 4KB
rand1.cpp 3KB
vecSubs.cpp 3KB
useScreen.cpp 3KB
usealloc.cpp 3KB
String.cpp 3KB
unorderedWordCount.cpp 3KB
map1.cpp 3KB
Sales_data.cpp 3KB
elimDups.cpp 3KB
multimap.cpp 3KB
erase2.cpp 3KB
FolderMain.cpp 3KB
StrFolderMain.cpp 3KB
validatePhones.cpp 3KB
reverse.cpp 3KB
virtualsNscope.cpp 3KB
Basket.cpp 3KB
seek.cpp 3KB
arrRet.cpp 3KB
rand2.cpp 3KB
tuple.cpp 3KB
lambda.cpp 3KB
cstring_hdr.cpp 3KB
erase1.cpp 3KB
dyn_cast.cpp 3KB
setOps.cpp 3KB
HasPtrUseCount.cpp 3KB
vecScores.cpp 3KB
fcnptrRet.cpp 3KB
共 496 条
- 1
- 2
- 3
- 4
- 5
资源评论
幻世111
- 粉丝: 31
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- (源码)基于C++和HDF5的二维扩散模拟系统.zip
- 现代 Java - Java 8 指南.zip
- 灌篮高手 JavaScript.zip
- (源码)基于Java和C++的魔塔AI与可视化系统.zip
- (源码)基于ESP32的URock机器人控制系统.zip
- 深度学习(Python、C、C++、Java、Scala、Go).zip
- (源码)基于Spring Boot和Vue的社交平台系统.zip
- 深入理解Java函数式Smashing和Streams API.zip
- (源码)基于Spring Boot框架的酒店管理系统.zip
- 浏览 JavaScript 程序的语言和原理 45 节课程,+6 个小时的视频和 130 个笑话 .zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功