Thread: Processor handling of bitwise operators

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    29

    Processor handling of bitwise operators

    Can someone explain in what order a CPU would process the following arithmetic problem: 5 - (-9) = 14?
    Would the CPU recognize that the 'minus a minus' combination simply represents 5 + 9, and proceed with that addition, or would the CPU have to first calculate the 2's complement of -9, and then proceed to take the 2's complement of that first result in order to complete the calculation of the addition of the 'double negative'?

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    This:
    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int a = 5 -(-6);
        printf("%d", a);
        return 0;
    }
    As you can see the piece of code that you are talking about does not go beyond compilation.

    Code:
    .file    "main.c"
        .section    .rodata.str1.1,"aMS",@progbits,1
    .LC0:
        .string    "%d"
        .section    .text.startup,"ax",@progbits
        .p2align 4,,15
        .globl    main
        .type    main, @function
    main:
    .LFB11:
        .cfi_startproc
        subq    $8, %rsp
        .cfi_def_cfa_offset 16
        movl    $11, %esi
        movl    $.LC0, %edi
        xorl    %eax, %eax
        call    printf
        xorl    %eax, %eax
        addq    $8, %rsp
        .cfi_def_cfa_offset 8
        ret
        .cfi_endproc
    .LFE11:
        .size    main, .-main
        .ident    "GCC: (SUSE Linux) 4.8.1 20130909 [gcc-4_8-branch revision 202388]"
        .section    .note.GNU-stack,"",@progbits
    As a side-note, the CPU executes what is in the binary without any bias meaning if the binary dictates the process as a 'double minus' it will do a double minus, if the binary dictates the process as an 'addition' it will do an addition.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Compilers typically implement an optimisation known as constant folding, so an expression like 5 - (-9) would be replaced by 14 at compile time, hence at run time no further evaluation is done. If not, your latter scenario would be done, i.e., -9 would be evaluated, then the overall expression would be evaluated. (Order of evaluation is generally unspecified, but here it is obvious.)

    Note that two's complement is not the only possible method of representation for signed integers permitted in C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Operators
    By (^Burt^) in forum C Programming
    Replies: 4
    Last Post: 10-24-2013, 01:20 AM
  2. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  3. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  4. bitwise operators?
    By EvilPickles in forum C++ Programming
    Replies: 18
    Last Post: 08-07-2006, 08:53 AM
  5. bitwise operators
    By Qasim in forum C Programming
    Replies: 1
    Last Post: 08-31-2001, 09:23 AM