通过一些例子重拾一些记不清了的c、c++知识。
首先是位运算:
然后是一些其他语法、输入输出:
//N诺一天重拾C语言 WebSite: https://noobdream.com//Major/majorinfo/1/
//目录:
//1:枚举类型+switch语句
//2:枚举类型
//3:位运算(原码、反码、补码)
//4:bool变量、逻辑运算 ×
//5:字符串
//6:结构体与链表
//7:qsort排序与rand随机技巧
//参考
//https://learn.microsoft.com/zh-cn/cpp/c-language/c-bitwise-operators?view=msvc-170
//https://blog.csdn.net/weixin_45728409/article/details/124492521
//
//负数的补码,是能够和其相反数相加通过溢出从而使计算机内计算结果变为0的二进制码。
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main(){
//switch与枚举类型
enum Day{
Mon =1,
Tue,
Wed,
Thur,
Fri,
Sat,
Sun
};
enum Day d = Mon;
cout<<d<<endl;
cout<<"请输入星期:"<<endl;
//scanf("%u",&d);
cin>>(int&)d;
switch(d){
case Mon:
cout<<"星期一.."<<endl;
break;
case Tue:
cout<<"星期二.."<<endl;
break;
case Wed:
cout<<"星期三.."<<endl;
break;
case Thur:
cout<<"星期四.."<<endl;
break;
case Fri:
cout<<"星期五.."<<endl;
break;
case Sat:
cout<<"星期六.."<<endl;
break;
case Sun:
cout<<"星期天.."<<endl;
break;
default:
cout<<"耶稣没预料到这天.."<<endl;
break;
};
//位运算:按位取反~、按位与&、按位或|、按位异或^,左移<<、右移>>
int num1,num2;
num1 = 1;//原码:0001,补码:0001(负数补码为反码符号位+1)(反码为原码除符号位外按位取反)
cout<<num1<<endl;
//详细解释见Ref 2
cout<<~num1<<endl; //补码存储,故不再取反加一:0001->1110 ->1001+1=1010,对原码取反,按补码输出
num2=0b001;
cout<<~num2<<endl;
int num3 = 0b01110000; //112;
int num4 = 0b01110001; //113;
cout<<num3<<endl;
cout<<num4<<endl;
cout<<(num3&1)<<endl; //偶数
cout<<(num4&1)<<endl;
cout<<"AA"<<endl;
int num5 = 0b00000110;
cout<<~num5<<endl;
int num7 = -8;
cout<<(num7^1)<<endl;
int num8 = 3;
cout<<(num8 << 1)<<endl;
int num9 = -5;
cout<<(num9 >> 1)<<endl;
//字符串
char str[]="";
cin>>str;
cout<<str<<endl;
int i=0;
while(str[i]!='\0'){
i++;
}
cout<<i<<endl;
string s;
cin>>s;
cout<<s<<endl;
cout<<s.length()<<endl;
return 0;
}
不会吧,不会有人机试爆零吧~
参考文献
C 按位运算符 | Microsoft Learn
C语言中按位取反操作符详解_c语言按位取反_LoganYang__的博客-CSDN博客