Skip to content

subtract

Mallikarjunarao Kosuri edited this page Jul 14, 2024 · 1 revision

The subtract function performs subtraction of two 8-bit signed integers using bitwise operations. This function does not use the subtraction operator directly; instead, it uses bitwise operations to achieve the same result.

Function Code

int8_t subtract(int8_t x, int8_t y)
{
    while (y != 0) {
        int8_t borrow = (~x) & y;
        x = x ^ y;
        y = borrow << 1;
    }
    return x;
}

How It Works

  1. Borrow Calculation:

    • int8_t borrow = (~x) & y;
    • Calculate the borrow where ~x (bitwise NOT of x) identifies the bits where x has 0s and y has 1s, which requires borrowing.
  2. Subtraction:

    • x = x ^ y;
    • Perform the bitwise XOR operation between x and y to calculate the intermediate subtraction result without considering the borrow.
  3. Shift Borrow:

    • y = borrow << 1;
    • Left shift the borrow by 1 to move it to the next significant bit.
  4. Loop Until Borrow is Zero:

    • Continue the process until the borrow becomes zero.

Example

Let's consider an example where x = 10 and y = 3.

  1. Binary Representation:

    • x = 10 (binary 00001010)
    • y = 3 (binary 00000011)
  2. Step-by-Step Execution:

  • Initial Values:
    • x = 10 (binary 00001010)
    • y = 3 (binary 00000011)
while (y != 0) {
    int8_t borrow = (~x) & y;
    x = x ^ y;
    y = borrow << 1;
}
  • First Iteration:

    • borrow = (~x) & y = (~00001010) & 00000011 = 11110101 & 00000011 = 00000001
    • x = x ^ y = 00001010 ^ 00000011 = 00001001
    • y = borrow << 1 = 00000001 << 1 = 00000010
  • Second Iteration:

    • borrow = (~x) & y = (~00001001) & 00000010 = 11110110 & 00000010 = 00000010
    • x = x ^ y = 00001001 ^ 00000010 = 00001011
    • y = borrow << 1 = 00000010 << 1 = 00000100
  • Third Iteration:

    • borrow = (~x) & y = (~00001011) & 00000100 = 11110100 & 00000100 = 00000100
    • x = x ^ y = 00001011 ^ 00000100 = 00001111
    • y = borrow << 1 = 00000100 << 1 = 00001000
  • Fourth Iteration:

    • borrow = (~x) & y = (~00001111) & 00001000 = 11110000 & 00001000 = 00000000
    • x = x ^ y = 00001111 ^ 00001000 = 00000111
    • y = borrow << 1 = 00000000 << 1 = 00000000
  • Result:

    • The loop terminates as y becomes 0.
    • The function returns x = 7.

Textual Diagram

Initial Values:
x =  10 (binary 00001010)
y =  3  (binary 00000011)

First Iteration:
~x          : 11110101
y           : 00000011
borrow      : 00000001
x ^ y       : 00001001
borrow << 1 : 00000010

Second Iteration:
~x          : 11110110
y           : 00000010
borrow      : 00000010
x ^ y       : 00001011
borrow << 1 : 00000100

Third Iteration:
~x          : 11110100
y           : 00000100
borrow      : 00000100
x ^ y       : 00001111
borrow << 1 : 00001000

Fourth Iteration:
~x          : 11110000
y           : 00001000
borrow      : 00000000
x ^ y       : 00000111
borrow << 1 : 00000000

Result: 7

Use Cases

When to Use

  • Basic Arithmetic: When subtraction is required and direct arithmetic operations are not available or need to be avoided.
  • Embedded Systems: In low-level programming where bitwise operations can offer better performance or are necessary due to hardware constraints.

Use Cases in Embedded Systems

  • Arithmetic Operations: Implementing basic arithmetic operations in microcontrollers that may lack hardware support for subtraction.
  • Optimized Performance: Bitwise operations can be more efficient in certain situations, offering faster execution and lower power consumption.
  • Digital Signal Processing: In DSP applications, bitwise operations are commonly used for arithmetic operations.
Clone this wiki locally