按位运算符
<< >> ~ & | ^ 一共6个
移位运算符
value<<shift;
13<<3; 13*2^3;
逻辑按位运算符
- !非运算符:将true或者非零值转为false,将false值转为true
- ~位非(取反)运算符。将其值的二进制0-》1 1-》0;
usigned char x = 3; 0000 0011 ->1111 1100 252 - 按位运算符(or)| , 两个数有1就为1.
- 按位运算符(xor)^ ,相同为0,不同为1
- 按位运算符(AND)& ,都为1才1
按位运算的替代表示
常用的按位运算技术
成员解除引用运算符
C++允许定义指向类成员的指针,对这种指针进行声明或解除引用时,需要用一种特殊的语法。
// class.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<string>
using namespace std;
class Example {
private:
int feet;
int inches;
public:
Example() { feet = 0; inches = 0; };
Example(int ft) { feet = ft; inches = 12 * feet; };
~Example() {};
void Show_in()const { cout <<"inches :"<< inches << endl; };
void Show_ft()const { cout << "feet :" << feet << endl; };
void use_ptr()const;
};
void Example::use_ptr()const
{
Example yard(3);
int Example::* pt;
pt = &Example::inches;
cout << "set pt to &Example::inches:\n";
cout << "this->*pt :" << this->*pt << endl;
cout << "yard.*pt :" << yard.*pt << endl;
void (Example:: * pf)()const;
pf = &Example::Show_in;
cout << "set pf to &Example::Show_in\n ";
cout << "(this->*pf):";
(this->*pf)();
cout << "(yard.*pf)();:";
(yard.*pf)();
}
int main()
{
Example car(15);
Example van(20);
Example garage;
cout << "use car :\n";
car.use_ptr();
cout << "\n van use car :\n";
van.use_ptr();
}
alignof(c++11)
// class.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<string>
using namespace std;
struct things1
{
char ch;
int a;
double x;
};
struct things2
{
int a;
double x;
char ch;
};
int main()
{
things1 th1;
things2 th2;
cout << "char alignment :" << alignof(char) <<endl; //1
cout << "int alignment :" << alignof(int) << endl;//4
cout << "double alignment :" << alignof(double) << endl;//8
cout << "things1 alignment :" << alignof(things1) << endl;//8
cout << "things2 alignment :" << alignof(things2) << endl;//8
cout << "sizeof things1 :" << sizeof(things1) << endl;//16
cout << "sizeof things2 :" << sizeof(things2) << endl;//24
}