//Exercise of 移位运算符
#include <iostream>
using namespace std;
int main()
{
unsigned int number{ 16387 };
unsigned int result{ number << 2 };//Shift left two bit positions.
cout << "After 16387 was shifted left two bit positions,the result is " << result << "." << endl;
unsigned int result1{};
result1 = number >> 2;
cout << "After 16387 was shifted right two bit positions,the result is " << result1 << "." << endl;
unsigned int number1{ 16387 };
number1 >>= 2;//is equal to number1 = number1 >> 2;
cout << "After 16387 was shifted right two bit positions,the result is " << number1 << "." << endl;
unsigned int umbrella{ 16387 };
cout << "After 16387 was shifted right two bit positions,the result is " << ( umbrella >> 2) << "." << endl;
//向右移位运算符在带符号整数类型的操作随编译器的不同而不同。在一些情况下,向右移位运算符会在左边空出来的位上填充0,
//在其他情况下,符号位向右移动,在左边空出来的位上填充1.
//移动符号位的原因是为了保持向右移位和除法运算的一致性。
//example1:signed char 类型的变量
signed char value{ -104 };//It's 1001 1000.(补码)
signed char value1{};
value1= value >> 2;//Result is 1110 0110.
//右边溢出了两个0,因为符号位是1,就在左边空出来的位上填充1,该结果是-26,正是value除以4的结果。
//对于不带符号的整数类型的操作,符号位不移动,在左边空出来的位上填充0.
cout << "The result is " << value1 << "." << endl;
system("pause");
}
问号表示最终的value1是个不可见字符啊,如果你想输出它的值,就不能定义为char类型,char类型变量默认输出是字符,不是ASCII码。你可以强制输出ASCII码, cout << "The result is " << (int)value1 << "." << endl;
ascii码没有对应的字符,就会用问号代替
还有一些其他ascii码有对应字符但是不可见,会什么都不输出
典型的比如一个空格,除非空格后面还有其他字符,你才能间接看见空格,否则不可见,还有类似\b,\r之类的操作符都不可见
如果你需要打印它的值而不是对应的字符,那么应该用printf("%d",value1)这样打印,或者先强制转换类型(int)value1
拆分为基本操作:遍历到第i个元素的位置,在它后面插入新的元素
void insElem(node *&head,int value,int i){ //在第i个元素后插入值为value的结点
node *p=head->next; //工作指针
int count=1;
while(p!=NULL&&count!=i){
p=p->next;
count++;
}
node *q=new node(); //创建
q->data=value;
q->next=p->next;
p->next=q;
}
ps. i=0
时在头结点后直接插入结点