avatar
fireworks99
keep hungry keep foolish

今日份的奇怪小发现

Interesting

  1. 0x7fffffff = 2 ^ 31 - 1 (MAX_INT)
  2. 0x3f3f3f3f ≈ 1e9 (略小于MAX_INT的一半)
  3. char 与 int 作四则运算时char类型自动转为其对应Ascall码值大小的int值
  4. 形参为string字符串的函数,可以传入char s字符数组去执行(看来字符数组等级高于字符串,字符数组可*自动转字符串)
  5. 异或交换两个整数值的方法:不用swap,不借助第三者

Code

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;

int sz(string s)
{
    return s.length();
}

int sszz(char * s)
{
    int num = 0;
    while(*s)
        num++, s++;
    return num;
}

int main()
{
    int a = 0x7fffffff;///2 ^ 31 - 1
    int b = 0x3f3f3f3f;/// ≈ 1e9
    cout << a << ' ' << b << '\n';

    ///char 与 int 作四则运算时
    ///char类型自动转为其对应Ascall码值大小的int值
    char ch = '0';
    cout << sizeof(ch) << '\n';     /// 1
    int c = 2;
    int d = c * ch;
    cout << d << '\n';              /// 96 = 2 * 48


    char e[100] = "ABCD";
    cout << sizeof(e) << '\n';      /// 100


    string str = "XYZ";
    cout << sz(str) << '\n';
//    cout << sszz(str) << '\n';    ///报错
    cout << sz(e) << '\n';
    cout << sszz(e) << '\n';
    return 0;
}

Code2

#include <ctime>
#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
    srand((time(NULL)));
    int a = rand();
    int b = rand();
    cout << a << ' ' << b << '\n';
    a ^= b;
    b ^= a;
    a ^= b;
    cout << a << ' ' << b << '\n';
    return 0;
}
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy