Two's Complement Representation
Understanding how computers represent negative numbers is crucial when working with low-level programming in C.
The most common way to represent signed integers is two's complement. In this system, the most significant bit (MSB) is the sign bit: 0 means non-negative, 1 means negative. Negative numbers are formed by inverting all bits of the positive value and adding 1.
Twoβs complement allows addition and subtraction to work the same way for both positive and negative numbers, which simplifies hardware design and arithmetic operations in software.
twos_complement_demo.c
123456789101112131415161718192021222324#include <stdio.h> #include <stdint.h> void print_binary(uint8_t value) { for (int i = 7; i >= 0; i--) { printf("%d", (value >> i) & 1); } printf("\n"); } int main(void) { int8_t positive = 25; int8_t negative = (int8_t)(~positive + 1); // two's complement printf("Positive value: %d\n", positive); printf("Binary: "); print_binary((uint8_t)positive); printf("\nNegative value: %d\n", negative); printf("Binary: "); print_binary((uint8_t)negative); return 0; }
To convert a positive number to its twoβs complement, start with the value, for example 25. First, invert all its bits using the bitwise NOT operator (~). Then add 1 to the result. This produces the binary representation of -25.
When you print both values in binary, you can see how twoβs complement allows the same arithmetic logic to work for positive and negative numbers, with carries and borrows handled automatically.
Two's complement is used by most modern systems, which is why signed integer arithmetic works consistently at both the hardware and software levels.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 6.25
Two's Complement Representation
Swipe to show menu
Understanding how computers represent negative numbers is crucial when working with low-level programming in C.
The most common way to represent signed integers is two's complement. In this system, the most significant bit (MSB) is the sign bit: 0 means non-negative, 1 means negative. Negative numbers are formed by inverting all bits of the positive value and adding 1.
Twoβs complement allows addition and subtraction to work the same way for both positive and negative numbers, which simplifies hardware design and arithmetic operations in software.
twos_complement_demo.c
123456789101112131415161718192021222324#include <stdio.h> #include <stdint.h> void print_binary(uint8_t value) { for (int i = 7; i >= 0; i--) { printf("%d", (value >> i) & 1); } printf("\n"); } int main(void) { int8_t positive = 25; int8_t negative = (int8_t)(~positive + 1); // two's complement printf("Positive value: %d\n", positive); printf("Binary: "); print_binary((uint8_t)positive); printf("\nNegative value: %d\n", negative); printf("Binary: "); print_binary((uint8_t)negative); return 0; }
To convert a positive number to its twoβs complement, start with the value, for example 25. First, invert all its bits using the bitwise NOT operator (~). Then add 1 to the result. This produces the binary representation of -25.
When you print both values in binary, you can see how twoβs complement allows the same arithmetic logic to work for positive and negative numbers, with carries and borrows handled automatically.
Two's complement is used by most modern systems, which is why signed integer arithmetic works consistently at both the hardware and software levels.
Thanks for your feedback!