《Beginning C++17》-学习笔记-Chapter 03-Working with Fundamental Data Types

Beginning C++ Through Game Progamming 全书学习笔记 Beginning C++ Through Came Programming 2017.03.14 - 2017.03.17 简单编程熟悉概念,四天全部看完。 (001)致谢 赠人玫瑰,手有余香 Finally, I want to thank all of the game programmers who created the games I played while growi 阅读详情

The XOR operator is used less frequently than the & and | operators. Important applications arise, however, in for instance cryptography, random number generation, and computer graphics. XOR is also used for the backup of hard disk data by certain RAID technologies. Suppose you have three similar hard drives, two with data and one to serve as backup. The basic idea is to ensure that the third drive at all times contains the XOR’ed bits of all contents of the two other drives, like so:
Drive one ... 1010 0111 0110 0011 ...
Drive two ... 0110 1100 0010 1000 ...
XOR drive (backup) ... 1100 1011 0100 1011 ...

//不借助第三个变量,利用exclusive or运算,交换两个变量的值
//给两个int变量赋值,交换这两个int变量的数值后输出这两个int变量的值;可用3, 14 两个整数验证
#include <iostream>

int main()
{
	int first{}, second{};
	std::cout << "Enter two integers separated by a space: ";
	std::cin >> first >> second;

	first ^= second;
	second ^= first;
	first ^= second;
	std::cout << "In reverse order they are " << first << " and " << second << std::endl;
}

 

#include <iostream>

int main()
{
	std::cout << sizeof(unsigned short) << std::endl;//输出2,说明unsigned short占2个byte, 即16 bit;
	std::cout << (0b0000000000000001 << 2) << std::endl;//输出结果为4,即0b0000000000000100
	std::cout << (0b0000000000000001 | 0b0000000000000010) << std::endl;//输出结果为3,即0b0000000000000011
	std::cout << ~(~0b0000000000000001);//数值不变,输出结果仍为1,即0b0000000000000001
}
/*It will be convenient to display the data as hexadecimal values, and inserting std::hex in the output
stream does this. The hex is modal, so all subsequent integer output will be in hexadecimal format. It will be
easier to compare output values if they have the same number of digits and leading zeros. You can arrange
for this by setting the fill character as 0 using the std::setfill() manipulator and ensuring the field width
for each output value is the number of hexadecimal digits, which is 8(其中一项的设为了16,以示区别). The setfill() manipulator is modal,
so it remains in effect until you reset it. The std::setw() manipulator is not modal; you have to insert it into
the stream before each output value.*/
#include <iostream>
#include <iomanip>

int main()
{

	std::cout << std::hex                     // Hexadecimal output
		<< std::setfill('0');           // Fill character 0

	unsigned int flags{ 0xFF };                // Flags variable
	unsigned int bit1mask{ 0x1 };              // Selects bit 1
	unsigned int bit6mask{ 0b100000 };         // Selects bit 6
	unsigned int bit20mask{ 1u << 19 };        // Selects bit 20

	std::cout << "Use masks to select or set a particular flag bit:";
	std::cout << "\nSelect bit 1 from flags  : " << std::setw(16) << (flags & bit1mask);
	std::cout << "\nSelect bit 6 from flags  : " << std::setw(8) << (flags & bit6mask);
	std::cout << "\nSwitch off bit 6 in flags: " << std::setw(8) << (flags &= ~bit6mask);
	std::cout << "\nSwitch on bit 20 in flags: " << std::setw(8) << (flags |= bit20mask)/*The parentheses around the expressions are necessary here because the precedence of << is higher than
& and |.*/
		<< std::endl;
}

 输出结果:


#include <iostream>
#include <iomanip>

int main()
{
	enum class Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };/*When you define an enumeration, you’re creating a new type, so it’s also referred to as an enumerated data type.This defines an enumerated data type called Day, and variables of this type can only have values from the set that appears between the braces. The symbolic names between the braces are called enumerators.*/
	Day yesterday{ Day::Monday };/*You use type Day just like any of the fundamental types. When you reference an enumerator, it must be qualified by the type name.*/
	const Day poets_day{ Day::Friday };


	enum class Punctuation : char { Comma = ',', Exclamation = '!', Question = '?' };/*The type specification for the enumerators goes after the enumeration type name and is separated from it by a colon. */
	Punctuation ch{ Punctuation::Comma };

	std::cout << "yesterday's value is " << static_cast<int>(yesterday) /*To output the value of today, you must cast it to a numeric type because the standard output stream will not recognize the type Day*/
		<< static_cast<char>(ch) << " but poets_day's is " << static_cast<int>(poets_day)
		<< static_cast<char>(Punctuation::Exclamation) << std::endl;

}
#include <iostream>

int main()
{
	using my_type_new = unsigned long long; /* The using keyword enables you to specify a type alias, which is your own data type name that serves as an alternative to an existing type name.*/

	typedef short int my_type_old;/*older syntax for defining an alias for a type name。在以前的代码中存在,不建议继续使用*/

	my_type_new new_type_val{ 1034 };
	my_type_old old_type_val{ 231 };

	std::cout << new_type_val << std::endl;
	std::cout << old_type_val << std::endl;

}

All variables have a finite lifetime. They come into existence from the point at which you define them, and at some point they are destroyed—at the latest, when your program ends. How long a particular variable lasts is determined by its storage duration. There are four different kinds of storage duration:

  1. Variables defined within a block that are not defined to be static have automatic storage duration. They exist from the point at which they are defined until the end of the block, which is the closing curly brace of the block that encloses its
    definition , }.
    They are referred to as automatic variables or local variables. Automatic variables are said to have local scope or block scope.
  2. Variables defined using the static keyword have static storage duration. They are called static variables. Static variables exist from the point at which they are defined and continue in existence until the program ends.
     
  3. Variables for which you allocate memory at runtime have dynamic storage duration. They exist from the point at which you create them until you release their memory to destroy them.
     
  4. Variables declared with the thread_local keyword have thread storage duration.

Another property that variables have is scope. The scope of a variable is the part of a program in which the variable name is valid. Within a variable’s scope, you can refer to it, set its value, or use it in an expression. Outside of its scope, you can’t refer to its name. Any attempt to do so will result in a compiler error message.

The lifetime and scope of a variable are different things. Lifetime is the period of execution time over which a variable survives. Scope is the region of program code over which the variable name can be used.



 You can define variables outside all of the functions in a program. Variables defined outside of all blocks and classes are also called globals and have global scope (which is also called global namespace scope).

Global variables have static storage duration by default, so they exist from the start of the program until execution of the program ends. If you don’t initialize a global variable, it will be zero-initialized by default. This is unlike automatic variables, which contain garbage values when uninitialized.

Only global variables have default initial values, which is 0, not automatic variables.

Global variables have a scope that extends from the point at which they’re defined to the end of the file.

Variables with global scope are accessible from anywhere within the program file that contains them, following the point at which they’re defined, except where a local variable exists with the same name as the global variable. Even then, they can still be reached by using the scope resolution operator (::).

#include <iostream> 

long count1{ 999L };         // Global count1

int count3;                // Global count3 - default intialization, = 0

int main()
{ 
	int count1{ 10 };          // local count1, hides global count1
	std::cout << "Value of outer count1 = " << count1 << std::endl;
	std::cout << "Value of global count1 = " << ::count1 << std::endl;//you must use the scope resolution operator :: to access the global count1 in the output statement
	std::cout <<"Value of outer count3 = " << count3 << std::endl;//

} 

// Exercise 3-1. Output integer and its 1's complement in decimal and hexadecimal.

// The setw() manipulator only applies to the next output value so you must use it preceding each output value. 
// std::internal causes the fill character, '0', to be used internal to the hexadecimal values.
// Because the program computes the complement of signed integers, 
// the output of this program is strictly speaking undefined.
// That is: it depends on which binary representation of negative integers your computer uses.
// Nearly all computers employ the two's complement encoding though,
// which means that flipping all bits and adding one should negate your number.

#include <iostream>
#include <iomanip>

int main()
{
	int value{};
	std::cout << "Enter any integer: ";
	std::cin >> value;
	int inverted_value{ ~value };

	unsigned int hex_digits{ 2 * sizeof(int) };          /*Hex digits in value;每个int类型的数值占4 bytes, 32 bits, 每个16进制的数字可以代表4个bit,所以, 1个32位的int类型数值可以用8个16进制数字表示*/
	unsigned int hex_value_width{ hex_digits + 2 };      // Add 2 for 0x prefix
	unsigned int column_width{ hex_value_width + 4 };    // Output column width (add 4 for padding)

	// Output column headings
	std::cout << std::right << std::setw(column_width) << "value"
		<< std::setw(column_width) << "~value"
		<< std::setw(column_width) << "~value+1" << std::endl;

	// Output hexadecimal values
	std::cout << std::hex << std::showbase << std::internal << std::setfill('0')
		<< "    " << std::setw(hex_value_width) << value
		<< "    " << std::setw(hex_value_width) << inverted_value
		<< "    " << std::setw(hex_value_width) << inverted_value + 1 << std::endl;

	// Output decimal values
	std::cout << std::dec << std::right << std::setfill(' ')
		<< std::setw(column_width) << value
		<< std::setw(column_width) << inverted_value
		<< std::setw(column_width) << inverted_value + 1 << std::endl;
}
// casting from double to long (using static_cast<>()) ensures that the fractional part of the double value is omitted.

#include <iostream>

int main()
{

	double double_num_1{ 12.345 };
	double double_num_2{ 12.567 };

	std::cout<< static_cast<long>(double_num_1) << std::endl;//输出12
	std::cout << static_cast<long>(double_num_2) << std::endl;//输出12

}

#include <iostream>

int main()
{

	auto j { ~(~0u << 3) };/*~0 is composed of all 1s; shifting that three places to the left and complementing the result will leave 111, which is 7*/

	std::cout << j << std::endl;//输出7
}
// This program will only work if a single byte is 8 bit (virtually always the case),
// and the fundamental type int counts at least 4 bytes (true for most modern systems).

#include <iostream>
#include <iomanip>

int main()
{
	unsigned int two_char_word{};
	unsigned char ch{};//用来临时存贮字符

	//输入两个字符,存到two_char_word
	std::cout << std::left << std::setw(26) << "Enter a character: ";
	std::cin >> ch;
	two_char_word |= ch;

	std::cout << std::setw(26) << "Enter a second character: ";
	std::cin >> ch;
	two_char_word <<= 8;                                // Shift left 1 byte
	two_char_word |= ch;

	std::cout << "The hexadecimal number of the word containing 2 characters is " << std::right
		<< std::hex << std::showbase << std::internal << std::setfill('0') << two_char_word << std::endl;

	std::cout << std::setfill(' ');

	// 将刚才的两个字符倒序输出
	unsigned int mask{ 0x000000FF };              // Keep low order byte
	ch = mask & two_char_word;                          // Low order byte
	std::cout << std::setw(4) << ch;
	ch = mask & (two_char_word >> 8);                   // 2nd byte
	std::cout << std::setw(4) << ch;

}

#include <iostream>
#include <iomanip>

int main()
{
	const unsigned R{ 0x00FF0000 };
	const unsigned G{ 0x0000FF00 };
	const unsigned B{ 0x000000FF };
	enum class Color : unsigned
	{
		Red = R,
		Green = G,
		Blue = B,
		Black = 0,
		White = R | G | B,
		Yellow = R | G,
		Purple = R | B,
	};

	const Color color1{ Color::Yellow };

	auto color{ static_cast<unsigned>(color1) };  // Get the enumerator value,这一步将color1的类型由Color转换成auto类型,然后进行下面的&运算
	
	std::cout << std::setw(38) << "The components of color1 (yellow) are:"
		<< "  Red:" << std::setw(3) << ((color & R) >> 16) //不能直接用color1 和 R做 & 运算,因为类型不一致,编译时会报错
		<< "  Green:" << std::setw(3) << ((color & G) >> 8)
		<< "  Blue:" << std::setw(3) << (color & B) << std::endl;

}

 

《Beginning C++17》-学习笔记-Chapter 01-Basic Ideas 书名:Beginning C++17 - From Novice to Professional 作者:Ivor Horton, Peter Van Weert 笔记: Blocks can be placed inside other blocks—this concept is called nesting. Blocks can be nested, one within anothe... 阅读详情

相关推荐

Beginning C++17 C++编程入门英文原版

Beginning C++17 C++编程入门英文原版。电子版仅供预览及学习交流使用,下载后请24小时内删除,支持正版,喜欢的请购买正版书籍。

《Beginning C++17》-学习笔记-Chapter 06-Pointers and References

#include &lt;iostream&gt; int main() { long* pnumber1{};//the statement initializes pnumber1 with the pointer equivalent of zero, which is a special address that doesn't point to anything. long* p...

CodingIsFun 396

Beginning C++17 From Novice to Professional, 5th edition.zip

Learn how to program using the updated C++17 language. You'll start with the basics and progress through step-by-step examples to become a working C++ programmer. All you need are Beginning C++17 and any recent C++ compiler and you'll soon be writing real C++ programs. There is no assumption of prior programming knowledge

C++17 C++0x 最新特性资源合集

深入应用C++11:代码优化与工程级应用 https://download.csdn.net/download/zang141588761/10646458 Sustainable Software Development Patterns and Best Practices with C++ 17 https://download.csdn.net/download/zang1415...

crkres9527 6427

Beginning C++17, 5th Edition

c++17 教材, Beginning C++17, 5th Edition Beginning C++17, 5th Edition

Beginning C++17: From Novice to Profession

Beginning C++17: From Novice to Professional, 5th edition English | ISBN: 1484233654 | 2018 | PDF+EPUB This latest edition has been fully updated to the latest version of the language, C++17, and to all conventions and best practices of so-called modern C++. Beginning C++17 also introduces the elements of the C++ Standard Library that provide essential support for the C++17 language.

C++17标准

C++17标准 中文翻译 译自C++17最终工作草案。 目录 单击这里。现为翻译草稿(未修订)。 维基百科 译自维基百科。 简介 C++17(或非正式名称,C++1z)是ISO/IEC 14882标准的最新版本的C++编程语言的名称。C++17标准在2017年3月达到国际标准草案(DIS)阶段。这一DIS得到了一致的认可,只有编辑者的评论,最终标...

北辰 9971

Beginning C++17 From Novice to Professional(5th) epub

Beginning C++17 From Novice to Professional(5th) 英文epub 第5版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

《Beginning C++17》-学习笔记-Chapter 09-Functions Templates

// Using a function template #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; template&lt;typename T&gt; T larger(T a, T b); // Function template prototype // a speciali...

CodingIsFun 458

Beginning C++17_From Novice to Professional-Apress(2018).pdf

Welcome to Beginning C++17. This is a revised and updated version of Ivor Horton’s original book called Beginning ANSI C++. The C++ language has been extended and improved considerably since then, so much so that it was no longer possible to squeeze detailed explanations of all of C++ into a single book. This tutorial will teach the essentials of the C++ language and Standard Library features, which will be more than enough for you to write your own C++ applications. With the knowledge from this book, you should have no difficulty in extending the depth and scope of your C++ expertise. We have assumed no prior programming knowledge. If you are keen to learn and have an aptitude for thinking logically, getting a grip on C++ will be easier than you might imagine. By developing C++ skills, you’ll be learning a language that is already used by millions and that provides the capability for application development in just about any context. C++ is very powerful. Arguably, it’s more powerful than most programming languages. So, yes, like with any powerful tool you can wield some considerable damage if you use it without proper training. We often compare C++ to a Swiss Army knife: age-old, trusted, incredibly versatile, yet potentially mind-boggling and full of pointy things that could really hurt you. Once someone clearly explains to you what all the different tools are meant for, however, and teaches you some elementary knife safety rules, then you’ll never have to look for another pocketknife again. C++ does not need to be dangerous or difficult at all either. C++ today is much more accessible than many people assume. The language has come a long way since its conception nearly 40 years ago. In essence, we have learned how to wield all its mighty blades and tools in the safest and most effective way possible. And, more importantly perhaps, the C++ language and its Standard Library have evolved accordingly to facilitate this. The past decade in particular has seen the ris

基于单片机红外线遥控Proteus仿真电路发射接收

*单片机设计介绍,基于单片机红外线遥控Proteus仿真电路发射接收。

2201_75940997的博客 3955

C++17 标准正式发布:开发者可更简单地编写和维护代码

昨日,ISO C++ 委员会正式发布了 C++ 17 标准,正式名称为 ISO/IEC 14882:2017。 基于 C++ 11,C++ 17 旨在使 C++ 成为一个不那么臃肿复杂的编程语言,以简化该语言的日常使用,使开发者可以更简单地编写和维护代码。 C++ 17 是对 C++ 语言的重大更新,引入了许多新的语言特性: UTF-8 字符文字 折叠表

开源中国(oschina)的博客 2586

安川机器人SOCKET 通讯

命令 功能 参数 示例 返回 SelectJob 选择程序 JobName LineNo SelectJob TEST 0 #SelectJob,Ok StartJob 启动程序 JobName StartJob TEST #StartJob,Ok Pause 暂停当前程序 - Pause #Pause,Ok Continue 继续当前程序 - Continue #Continue,Ok ServoOn 伺服上电 - ServoOn #ServoOn,Ok ServoOf...

记录学习内容 8308

C++ 17 标准正式发布:终于能更简单地编写和维护代码了!

点击上方“CSDN”,选择“置顶公众号” 关键时刻,第一时间送达! 何为 C++ 17? C++17 是继 C++14 之后,C++ 编程语言 ISO/IEC 标准的下一次修订的非正式名称。而就在昨日,ISO C++ 委员会正式发布了 C++ 17 标准,官方名称为 ISO/IEC 14882:2017。 C++ 17 标准化图表 C ++ 17 主要

CSDN资讯 7万+

新手秒变高手:使用一键脚本快速安装OpenClaw并完成初始化配置

一键脚本是一种自动化工具,可以帮助用户快速完成软件的安装和配置过程,无需手动执行繁琐的命令。OpenClaw提供了官方的一键安装脚本,让新手用户也能轻松部署OpenClaw。

一键难忘的博客 369

UNet 改进(19):与ASPP结合的图像分割模型

这个代码实现了一个改进版的UNet模型,在传统的UNet架构基础上,引入了ASPP模块来增强模型捕获多尺度上下文信息的能力。​​UNet主干网络​​:经典的编码器-解码器结构,带有跳跃连接​​ASPP模块​​:位于瓶颈层,用于提取多尺度特征。

2401_82355416的博客 885

《C++ Without Fear》学习笔记

书名:C++ Without Fear A Beginner’s Guide That Makes You Feel Smart 作者:Brian Overland 阅读时间:2018/10/6-2018/10/20 书摘+书评: An important rule of good programming style is that variables should usually...

CodingIsFun 832

深入理解Android音视频同步机制(三)NuPlayer的avsync逻辑

本文深入介绍了NuPlayer的avsync逻辑和原理

张晖的专栏 1万+
上一篇: 程序员笑话-持续更新中
下一篇: 《Beginning C++17》-学习笔记-Chapter 04-Making Decisions
CodingIsFun
博客等级 码龄8年 37粉丝 240原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址