分类: C++

  • 补码

    补码是计算机表示有符号整数的一种方式,核心作用是把 “减法” 变成 “加法”,让计算机只用 “加法器” 就能处理加减运算。下面用最通俗的方式解释:

    一、为什么需要补码?

    计算机里用二进制存数,有符号数的最高位是符号位0 表示正数,1 表示负数)。如果直接用 “符号位 + 原码”(比如 +3 是 0011-3 是 1011),做减法会很麻烦(比如 3-2 要专门设计减法逻辑)。

    补码的巧妙之处:用 “加法” 代替 “减法”。比如 3-2 可以变成 3 + (-2),只要给 -2 一个特殊的 “补码”,让加法的结果等价于减法,计算机就只需 “加法器” 即可。

    二、补码的计算规则

    • 正数的补码:和原码完全相同
      例:+5 的原码是 00000101(8 位),补码也是 00000101
    • 负数的补码:分两步(或用 “模” 理解):
      • 方法 1(步骤法):原码的符号位不变数值位取反,然后加 1
        例:求 -5 的补码:
        ① 原码:10000101(符号位 1,数值位 0000101);
        ② 数值位取反:1111010
        ③ 加 1:1111011
        ④ 最终补码:11111011(符号位保留 1)。
      • 方法 2(模的概念):补码 = 模 – 数的绝对值(更直观)。
        对于 8 位二进制,“模” 是 2⁸ = 256(因为 8 位最多表示 256 个数)。
        所以 -5 的补码 = 256 - 5 = 251,二进制是 11111011(和步骤法结果一致)。

    三、补码如何让 “减法变加法”?

    以 3-2 为例,等价于 3 + (-2)

    • 3 的补码:00000011(正数,补码 = 原码);
    • -2 的补码:用 “模 256 – 2” 得 254,二进制 11111110
    • 两者相加:00000011 + 11111110 = 100000001
    • 因为是 8 位,最高位的 1 会 “溢出”(超出 8 位范围),最终结果取低 8 位 00000001,即 1,和 3-2=1 一致。

    四、特殊情况:-128(8 位补码)

    8 位补码的范围是 -128 ~ 127。其中 -128 比较特殊:它没有 “原码”(因为原码表示范围是 -127 ~ 127),但补码可以表示为 10000000(用 “模 256 – 128 = 128”,二进制 10000000)。

    总结

    补码的核心是用加法代替减法,让计算机硬件更简单。记住:

    • 正数补码 = 原码;
    • 负数补码 = 模 – 绝对值(或 “符号位不变,数值位取反加 1”);
    • 计算时,直接用补码相加,结果自然等价于原数的加减。
  • #C++ Day2 Sep 10 2025

    #include <iostream>

    using namespace std;

    #define X “Vito写算法”

    #define Y “Deep Night”

    int main() {

    cout <<X <<endl <<Y<<endl ;

    //define constant can substitute sth

    }

    #include <iostream>

    using namespace std;

    #define X “Vito写算法”

    #define Y “Deep Night”

    #define Z 1+2

    #define G (1+2)

    const int x = 8;

    int main() {

    cout <<X <<endl <<Y<<endl ;

    cout << Z*Z<<endl; //would be 5,cause 1+2*1+2=5

    cout << G * G<< endl; // would be 9,cause you add the ()

    //x = (x + 1); //if add up the const ,this x can’t be modify

    //c++ is  upper and lower case sensitive 

    cout << x << endl;

    }

    #include <iostream>

    using namespace std;

    int main(){

    //int float;//it is wrong because you can’t use the reserve key as the symbol

    int a = 5;

    if (a == 6) {

    return -1;

    }

    return -2;

    }

    #include <iostream>

    using namespace std;

    int main() {

    int a12_ = 520;

    //int 9b_ = 22; //number can’t be the start of the literal

    int a = 5;

    int A = 8;

    cout << a << endl << A <<endl;

    int appleCount = 0;

    int chuangshijiejing = 0; //pinyin can be normal if you are in the chinese group

    #include <iostream>

    using namespace std;

    int main() {

    int a12_ = 520;

    //int 9b_ = 22; //number can’t be the start of the literal

    int a = 5;

    int A = 8;

    cout << a << endl << A <<endl;

    int appleCount = 0;

    int chuangshijiejing = 0; //pinyin can be normal if you are in the chinese group

    //variety Literal of C++

    /*1.case sensitive

    2.can not be started with number

    3.can not be the system reserve literal

    4.you can use word,number,_ as the variety Literal*/

    }

  • #C++ Day1 Sep 7 2025

    1. Leant how to install the environment 
    2. The dream is to code a game for me
    3. Found the reason of headache:fit the brain 
    4. Not the copy&paste, if you write more, you remember more
    5. Try to answer the question  by yourself, do not to ask other people the first time

    #include <iostream>

    using namespace std;

    int main()

    {

    cout << “Vito Algorithm” << endl <<endl <<endl; //endl is change the line

    return -12;

    }

    #include <iostream>

    //using namespace std;

    int main()

    {

    std::cout << “Vito Algorithm” << std::endl << std::endl << std::endl; //endl is change the line

    // i can use std:: before the cout and endl, so that i don’t need to script namespace

    //the namespace, which is used to divide the different codes that uses the same variety name 

    /*

    multiple notes

    */

    return -12;

    }

    #include <iostream>

    using namespace std;

    int main()

    {

    //std::cout << “Vito Algorithm” << std::endl << std::endl << std::endl; //endl is change the line

    //the definition of the variety:type name =origin value;

    //please give the variaty a origin value, if you didn’t do it, you are making errors when you are cout

    int a = 520;

    int b;

    cout << a << endl;

    cout << b << endl;

    return 0;

    }