使用C ++ 20进行位操作

2020-12-13 17:22:22

这篇文章总结了我对C ++ 20库功能的介绍。今天,我写关于类std :: source_location和一些用于位操作的函数的文章。

std :: source_location表示有关源代码的信息。该信息包括文件名,行号和函数名。当您需要有关呼叫站点的信息(例如用于调试,记录或测试目的)时,此信息非常宝贵。 std :: source_location类是预定义的C ++ 11宏__FILE__和__LINE__的更好替代方法,因此应使用。

调用std :: source_location :: current()创建一个新的源位置对象src。 src代表呼叫站点的信息。现在,没有C ++编译器支持std :: source_location。因此,以下程序sourceLocation.cpp来自cppreference.com/source_location。

// sourceLocation.cpp //来自cppreference.com #include< iostream> #include< string_view> #include< source_location> 无效日志(std :: string_view消息, const std :: source_location& location = std :: source_location :: current()) { std :: cout<< " info:" << location.file_name()<< ':' << location.line()<< ' ' <<消息<< ' \ n&#39 ;; } int main() { log(" Hello world!"); // info:main.cpp:19您好,世界! }

字节序可以是大字节序或小字节序。 Big-endian表示最高有效字节在前; little-endian表示最低有效字节在前。

标量类型可以是算术类型,枚举,指针,成员指针或std :: nullptr_t。

如果所有标量类型的大小都为1,则字节序无关紧要;枚举数std :: endian :: little,std :: endian :: big和std :: endian :: native的值相同。

当我在x86体系结构上执行以下程序getEndianness.cpp时,得到的答案是little-endian。

// getEndianness.cpp #include< bit> #include< iostream> int main(){ 如果constexpr(std :: endian :: native == std :: endian :: big){ std :: cout<< " big-endian" << ' \ n&#39 ;; } 否则,如果constexpr(std :: endian :: native == std :: endian :: little){ std :: cout<< "小尾数" << ' \ n&#39 ;; //小尾数 } }

constexpr如果启用它可以有条件地编译源代码。这意味着编译取决于您的体系结构的字节序。如果您想进一步了解字节序,请阅读同名的Wikipedia页面。

除std :: bit_cast之外的函数均要求无符号整数类型(无符号字符,无符号短字符,无符号int,无符号长或无符号长整型)。

// bit.cpp #include< bit> #include< bitset> #include< iostream> int main(){ std :: uint8_t num = 0b00110010; std :: cout<< std :: boolalpha; std :: cout<< " std :: has_single_bit(0b00110010):" << std :: has_single_bit(num) << ' \ n&#39 ;; std :: cout<< " std :: bit_ceil(0b00110010):" << std :: bitset< 8>(std :: bit_ceil(num)) << ' \ n&#39 ;; std :: cout<< " std :: bit_floor(0b00110010):" << std :: bitset< 8>(std :: bit_floor(num))<< ' \ n&#39 ;; std :: cout<< " std :: bit_width(5u):" << std :: bit_width(5u)<< ' \ n&#39 ;; std :: cout<< " std :: rotl(0b00110010,2):" << std :: bitset< 8>(std :: rotl(num,2)) << ' \ n&#39 ;; std :: cout<< " std :: rotr(0b00110010,2):" << std :: bitset< 8>(std :: rotr(num,2)) << ' \ n&#39 ;; std :: cout<< " std :: countl_zero(0b00110010):" << std :: countl_zero(num)<< ' \ n&#39 ;; std :: cout<< " std :: countl_one(0b00110010):" << std :: countl_one(num)<< ' \ n&#39 ;; std :: cout<< " std :: countr_zero(0b00110010):" << std :: countr_zero(num)<< ' \ n&#39 ;; std :: cout<< " std :: countr_one(0b00110010):" << std :: countr_one(num)<< ' \ n&#39 ;; std :: cout<< " std :: popcount(0b00110010):" << std :: popcount(num)<< ' \ n&#39 ;; }

下一个程序显示2到7的函数std :: bit_floor,std :: bit_ceil,std :: bit_width和std :: bit_popcount的应用程序和输出。

// bitFloorCeil.cpp #include< bit> #include< bitset> #include< iostream> int main(){ std :: cout<< std :: endl; std :: cout<< std :: boolalpha; 对于(auto i = 2u; i< 8u; ++ i){ std :: cout<< " bit_floor("< std :: bitset< 8>(i)<<")=" << std :: bit_floor(i)<< ' \ n&#39 ;; std :: cout<< " bit_ceil("< std :: bitset< 8>(i)<<")=" << std :: bit_ceil(i)<< ' \ n&#39 ;; std :: cout<< " bit_width("< std :: bitset< 8>(i)<<")=" << std :: bit_width(i)<< ' \ n&#39 ;; std :: cout<< " bit_popcount("< std :: bitset< 8>(i)<<")=" << std :: popcount(i)<< ' \ n&#39 ;; std :: cout<< std :: endl; } std :: cout<< std :: endl; }

除了协程外,C ++ 20还提供了许多并发功能。首先,C ++ 20具有新的原子。存在用于浮点值和智能指针的新原子。 C ++ 20还允许等待原子。为了协调线程,信号量,闩锁和屏障开始起作用。另外,std :: thread也通过std :: jthread进行了改进。 std :: jthread的执行可以被中断并自动加入其析构函数中。

非常感谢我的Patreon支持者:Matt Braun,Roman Postanciuc,Tobias Zindl,Marko,G Prvulovic,ReinholdDröge,Abernitzke,Frank Grimm,Sakib,Broeserl,AntónioPina,Darshan Mody,Sergey Agafyin,АндрейБурмистров Lawton Shoemake,Animus24,Jozo Leko,John Breland,espkk,WolfgangGärtner,Louis St-Amour,Stephan Roslen,Venkat Nandam,Jose Francisco,Douglas Tinkham,Kuchlong Kuchlong,Avi Kohn,Robert Blanch,Truels Wissneth,Kris Kafka,Mario Luoni ,Neil Wang,Friedrich Huber,Sudhakar Balagurusamy,lennonli和Pramod Tikare Muralidhara。

我很乐意在全球范围内举办在线研讨会或面对面的研讨会。如果您有任何疑问,请给我打电话。

C ++ 20-深入分析:2021年2月1日-2021年2月3日(UTC 16:00-20:00)

这是我的标准研讨会的汇总。这些研讨会仅是为了给您一个第一方向。