C语言练习 — 2(C language practice — 2)

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

int main()
{
    int a = 5 % 2;//商2余1  取模分子分母都要为整数
    printf("a = %d\n",a);
    return 0;
}


int main()
{
    int a = -1;
    //>> -- 右移操作符
    //移动的是二进制位
    // 1.算术右移,左边补原符号位
    // 2.逻辑右移,左边补0
    //00000000000000000000000000010000
    //整数的二进制表示:原码、反码、补码
    //存储到内存的是补码
    //100000000000000000000001 - 原码
    //111111111111111111111110 - 反码
    //111111111111111111111111 - 补码
    int b = a >> 1;
    printf("%d\n", b);
    return 0;
}


int main()
{
    int a = -5;
    int b = a << 1;
    //左移操作符
    //左边丢弃,右边补0
    printf("%d\n", b);
    return 0;
}


int main()
{
    //& - 按位与
    int a = 3;
    int b = 5;
    int c = a & b;
    printf("%d\n", c);
    
    int a = 3;
    int b = 5;
    int c = a | b;
    printf("%d\n", c);

    int a = 3;
    int b = 5;
    int c = a ^ b;
    printf("%d\n", c);
    return 0;
}

int main()
{
    int a = 3;
    int b = 5;
    int tmp = 0;
    printf("before: a=%d b=%d\n", a, b);
    tmp = a;
    a = b;
    b = tmp;
    printf("after:  a=%d b=%d\n", a, b);
    return 0;
}

int main()
{
    int a = 3;
    int b = 5;
    printf("before: a=%d b=%d\n", a, b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("after:  a=%d b=%d\n", a, b);
    return 0;
}

int main()
{
    int a = 3;
    int b = 5;
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    printf("a = %d b = %d\n", a, b);
    return 0;
}


int main()
{
    int num = 0;
    int count = 0;
    scanf("%d", &num);
    while(num)
    {
        if (num % 2 == 1)
        {
            count++;
        }
        num = num / 2;
    }
    printf("%d\n", count);
    return 0;
}
存在问题,负数不对

0000000000000000000000000011
0000000000000000000000000001
0000000000000000000000000001
int main()
{
    int num = 0;
    int count = 0;
    scanf("%d", &num);
    int i = 0;
    for (i = 0; i < 32; i++)
    {
        if (1 == ((num >> i)&1))
            count++;
    }
    printf("%d", count);
    return 0;
}


还有方法或许是直接相加
int main()
{
    int num = 0;
    scanf("%d", &num);
    for (num = 0; num < 32:i++)
    {
        num = 
    }


int main()
{
    int a = 10;
    printf("%d\n", !a);//单目操作符
    return 0;
}

int main()
{
    int a = 10;
    int* p = &a;//取地址操作符
    *p = 20;//解引用操作符
    printf("%d\n", a);
    return 0;
}

int main()
{
    int a = 10;
    char c = 'r';
    char* p = &c;
    int arr[10] = { 0 };
    printf("%d\n", sizeof(a));
    printf("%d\n", sizeof(int));

    printf("%d\n", sizeof(c));
    printf("%d\n", sizeof(char));

    printf("%d\n", sizeof(p));
    printf("%d\n", sizeof(char*));

    printf("%d\n", sizeof(arr));
    printf("%d\n", sizeof(int [10]));
    return 0;
}
————————
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

int main()
{
    int a = 5 % 2;//商2余1  取模分子分母都要为整数
    printf("a = %d\n",a);
    return 0;
}


int main()
{
    int a = -1;
    //>> -- 右移操作符
    //移动的是二进制位
    // 1.算术右移,左边补原符号位
    // 2.逻辑右移,左边补0
    //00000000000000000000000000010000
    //整数的二进制表示:原码、反码、补码
    //存储到内存的是补码
    //100000000000000000000001 - 原码
    //111111111111111111111110 - 反码
    //111111111111111111111111 - 补码
    int b = a >> 1;
    printf("%d\n", b);
    return 0;
}


int main()
{
    int a = -5;
    int b = a << 1;
    //左移操作符
    //左边丢弃,右边补0
    printf("%d\n", b);
    return 0;
}


int main()
{
    //& - 按位与
    int a = 3;
    int b = 5;
    int c = a & b;
    printf("%d\n", c);
    
    int a = 3;
    int b = 5;
    int c = a | b;
    printf("%d\n", c);

    int a = 3;
    int b = 5;
    int c = a ^ b;
    printf("%d\n", c);
    return 0;
}

int main()
{
    int a = 3;
    int b = 5;
    int tmp = 0;
    printf("before: a=%d b=%d\n", a, b);
    tmp = a;
    a = b;
    b = tmp;
    printf("after:  a=%d b=%d\n", a, b);
    return 0;
}

int main()
{
    int a = 3;
    int b = 5;
    printf("before: a=%d b=%d\n", a, b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("after:  a=%d b=%d\n", a, b);
    return 0;
}

int main()
{
    int a = 3;
    int b = 5;
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    printf("a = %d b = %d\n", a, b);
    return 0;
}


int main()
{
    int num = 0;
    int count = 0;
    scanf("%d", &num);
    while(num)
    {
        if (num % 2 == 1)
        {
            count++;
        }
        num = num / 2;
    }
    printf("%d\n", count);
    return 0;
}
存在问题,负数不对

0000000000000000000000000011
0000000000000000000000000001
0000000000000000000000000001
int main()
{
    int num = 0;
    int count = 0;
    scanf("%d", &num);
    int i = 0;
    for (i = 0; i < 32; i++)
    {
        if (1 == ((num >> i)&1))
            count++;
    }
    printf("%d", count);
    return 0;
}


还有方法或许是直接相加
int main()
{
    int num = 0;
    scanf("%d", &num);
    for (num = 0; num < 32:i++)
    {
        num = 
    }


int main()
{
    int a = 10;
    printf("%d\n", !a);//单目操作符
    return 0;
}

int main()
{
    int a = 10;
    int* p = &a;//取地址操作符
    *p = 20;//解引用操作符
    printf("%d\n", a);
    return 0;
}

int main()
{
    int a = 10;
    char c = 'r';
    char* p = &c;
    int arr[10] = { 0 };
    printf("%d\n", sizeof(a));
    printf("%d\n", sizeof(int));

    printf("%d\n", sizeof(c));
    printf("%d\n", sizeof(char));

    printf("%d\n", sizeof(p));
    printf("%d\n", sizeof(char*));

    printf("%d\n", sizeof(arr));
    printf("%d\n", sizeof(int [10]));
    return 0;
}