How to convert negative integer to binary using 2’s complement

Introduction

In this blog, I explain in brief about Two's complement and how to convert a negative integer to binary string representation. This blog extends from the previous, How to convert unsigned integer to binary string representation

Concept

Two's complement is used to represent signed integers in binary. This page explains really well why we need 2's complement instead of using the most significant bit to represent negative values or another method.

How to convert a negative decimal to binary using 2's complement

  1. Use absolute value and convert to binary:
    • Represented the absolute value as usual in binary (e.g., +2 is 0000 0010 in 8 bits).
  2. Convert to negative:
    • Invert the bits: Change all 0s to 1s and all 1s to 0s (e.g., -2's bitwise inversion of 0000 0010 is 1111 1101).
    • Add 1: Add 1 to the inverted bits (e.g., 1111 1101 + 0000 0001 = 1111 1110).

Example:

  • +2 in binary (8 bits)0000 0010
  • -2 in two's complement (8 bits)1111 1110

Notes:

  • 1 's complement of a number is invert of every of its bits: ~(num)
  • 2's complement of a number is ~(num+1) or (-num)

Convert a negative integer to binary

Naive method

  • get the absolute value of the number.
  • convert to string binary representation using modulo
  • invert the bits
  • add 1.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

const int BITS = 32;

// Function to convert a non-negative integer to its binary representation using modulo method
std::string intToBinary(unsigned int num) {
    if (num == 0) return "0";

    std::string binary = "";
    while (num > 0) {
        int remainder = num % 2;
        binary += (remainder == 1) ? '1' : '0';
        num /= 2;
    }

    // Reverse the string to get the correct binary representation
    std::reverse(binary.begin(), binary.end());
    
    // Ensure the binary string has BITS length
    while (binary.length() < BITS) {
        binary = '0' + binary;
    }
    
    return binary;
}

// Function to find the two's complement of an integer
std::string findTwosComplement(int num) {
    unsigned int absNum = abs(num);

    std::string binary = intToBinary(absNum);
    cout << "Binary representation of " << num << " is              " << binary << endl;

    // Invert bits
    for (int i = 0; i < BITS; i++) {
        binary[i] = (binary[i] == '1') ? '0' : '1';
    }
    cout << "Inversion of " << num << " is                          " << binary << endl;

    // Add 1 to get the two's complement
    bool carry = true;
    for (int i = BITS - 1; i >= 0; i--) {
        if (carry) {
            if (binary[i] == '0') {
                binary[i] = '1';
                carry = false; // No more carry needed
            } else {
                binary[i] = '0'; // Keep carry
            }
        }
    }

    return binary;
}

int main() {
    int num = -2;
    std::string binaryRepresentation = findTwosComplement(num);
    std::cout << "Two's complement of " << num << " in binary is         " << binaryRepresentation << std::endl;
    return 0;
}

Output:

Binary representation of 2 is          00000000000000000000000000000010
Inversion of 2 is                      11111111111111111111111111111101
Two's complement of -2 in binary is    11111111111111111111111111111110

Bit manipulation method:

This simple program, same as in this blog, also gives 2's complement representation of negative number:

#include <iostream>
#include <string>

const int BITS = 32; // Assume 32-bit integers

std::string intToBinary(int num) {
    
    std::string binary = "";
    for (int i = BITS - 1; i >= 0; i--) {
        binary += (num & (1 << i)) ? '1' : '0';
    }
    return binary; 
}

int main() {
    int num = -2;

    std::string binaryRepresentation = intToBinary(num);
    std::cout << "Binary representation of " << num << " is: " << binaryRepresentation << std::endl;

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *