###### tags: `leetcode` `learning` Why there are 32 bits? === from https://hackmd.io/qXIAl5DyRyuFL9uDSLUvhg?both ##### experiment Environment is on https://www.onlinegdb.com/online_c_compiler ```cpp= #include <stdio.h> void print2(int x) { for (int c = 31; c >= 0; c--) { int k = x >> c; if (k & 1) printf("1"); else printf("0"); } printf("\n"); } int getSum(int a, int b){ int direct_sum_without_carry = a ^ b; // XOR printf("before the while; XOR; direct_sum_without_carry %d\n", direct_sum_without_carry); print2(direct_sum_without_carry); int carry = a & b; // AND printf("before the while; AND; carry %d\n", carry); print2(carry); while (carry != 0){ a = direct_sum_without_carry; b = carry << 1; direct_sum_without_carry = a ^ b; // XOR printf(" direct_sum_without_carry %12d ", direct_sum_without_carry); print2(direct_sum_without_carry); carry = a & b; // AND printf(" carry %12d ", carry); print2(carry); } int direct_sum_with_all_carry = direct_sum_without_carry; return direct_sum_with_all_carry; } int main() { int a = -1; int b = 1; printf("%d\n", getSum(a,b)); return 0; } ``` This will work. It overflows and go back to 1. ```typescript= before the while; XOR; direct_sum_without_carry -2 11111111111111111111111111111110 before the while; AND; carry 1 00000000000000000000000000000001 direct_sum_without_carry -4 11111111111111111111111111111100 carry 2 00000000000000000000000000000010 direct_sum_without_carry -8 11111111111111111111111111111000 carry 4 00000000000000000000000000000100 direct_sum_without_carry -16 11111111111111111111111111110000 carry 8 00000000000000000000000000001000 direct_sum_without_carry -32 11111111111111111111111111100000 carry 16 00000000000000000000000000010000 direct_sum_without_carry -64 11111111111111111111111111000000 carry 32 00000000000000000000000000100000 direct_sum_without_carry -128 11111111111111111111111110000000 carry 64 00000000000000000000000001000000 direct_sum_without_carry -256 11111111111111111111111100000000 carry 128 00000000000000000000000010000000 direct_sum_without_carry -512 11111111111111111111111000000000 carry 256 00000000000000000000000100000000 direct_sum_without_carry -1024 11111111111111111111110000000000 carry 512 00000000000000000000001000000000 direct_sum_without_carry -2048 11111111111111111111100000000000 carry 1024 00000000000000000000010000000000 direct_sum_without_carry -4096 11111111111111111111000000000000 carry 2048 00000000000000000000100000000000 direct_sum_without_carry -8192 11111111111111111110000000000000 carry 4096 00000000000000000001000000000000 direct_sum_without_carry -16384 11111111111111111100000000000000 carry 8192 00000000000000000010000000000000 direct_sum_without_carry -32768 11111111111111111000000000000000 carry 16384 00000000000000000100000000000000 direct_sum_without_carry -65536 11111111111111110000000000000000 carry 32768 00000000000000001000000000000000 direct_sum_without_carry -131072 11111111111111100000000000000000 carry 65536 00000000000000010000000000000000 direct_sum_without_carry -262144 11111111111111000000000000000000 carry 131072 00000000000000100000000000000000 direct_sum_without_carry -524288 11111111111110000000000000000000 carry 262144 00000000000001000000000000000000 direct_sum_without_carry -1048576 11111111111100000000000000000000 carry 524288 00000000000010000000000000000000 direct_sum_without_carry -2097152 11111111111000000000000000000000 carry 1048576 00000000000100000000000000000000 direct_sum_without_carry -4194304 11111111110000000000000000000000 carry 2097152 00000000001000000000000000000000 direct_sum_without_carry -8388608 11111111100000000000000000000000 carry 4194304 00000000010000000000000000000000 direct_sum_without_carry -16777216 11111111000000000000000000000000 carry 8388608 00000000100000000000000000000000 direct_sum_without_carry -33554432 11111110000000000000000000000000 carry 16777216 00000001000000000000000000000000 direct_sum_without_carry -67108864 11111100000000000000000000000000 carry 33554432 00000010000000000000000000000000 direct_sum_without_carry -134217728 11111000000000000000000000000000 carry 67108864 00000100000000000000000000000000 direct_sum_without_carry -268435456 11110000000000000000000000000000 carry 134217728 00001000000000000000000000000000 direct_sum_without_carry -536870912 11100000000000000000000000000000 carry 268435456 00010000000000000000000000000000 direct_sum_without_carry -1073741824 11000000000000000000000000000000 carry 536870912 00100000000000000000000000000000 direct_sum_without_carry -2147483648 10000000000000000000000000000000 carry 1073741824 01000000000000000000000000000000 direct_sum_without_carry 0 00000000000000000000000000000000 carry -2147483648 10000000000000000000000000000000 direct_sum_without_carry 0 00000000000000000000000000000000 carry 0 00000000000000000000000000000000 0 ``` ##### Question I want to know how to determine that there will be 32 bits. ~~~By `sizeof(int)`, I get a `4`, which is 16 bits.~~~ By `sizeof(int)`, I get a `4`, which is 32 bits.