Absolute Value Function

This is a discussion on Absolute Value Function within the C Programming forums, part of the General Programming Boards category; Hi, the other day I saw an "exercise" of C. It was: Write an absolute value function for an int, ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    58

    Absolute Value Function

    Hi, the other day I saw an "exercise" of C.

    It was:

    Write an absolute value function for an int, that doesn't use if, ?:, asm or calls to other functions.

    It's been two weeks since i'm trying to do it but i can't find a way.

    Does anyone know how to do it?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yes.

    Hint: Down deep under the hood, to a computer, what makes a number negative or positive?

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    58
    I know, the firts bit.

    But i tried.


    What i do is:

    Code:
    x = (~x) +1;
    and that gives me the opposite.

    But i need the absolute for all vales, negative or positive. And i can't find a way.

    I tried this, it's a bit complicate:

    Code:
    return (x ^ (0xFFFFFFFF * (x>>31))) + (x>>31);

    Besides, the first bit is not actually the sign, but rather when the first bit is one you start at the lowest negative value and keep adding the other bits

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    That's because x is signed. For simplicity, create an unsigned int inside your function, and assign it the value of x.

    Your last return value should then work if you perform those operations on the unsigned version of x.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    58
    omg it's working
    Thanks

    source:

    Code:
    inline unsigned int aabs(int x)
    {
        unsigned int a;
        a=x;
    
        return (a ^ (0xFFFFFFFF * (a>>31))) + (a>>31);
    }

  6. #6
    Registered User
    Join Date
    May 2007
    Location
    Australia, Melbourne.
    Posts
    5
    if the question says use bit wise operations then u already have a solution. Well if you dont want to use if..else there is another operator called the conditional operator that ?: ... if the question relates to that then this might just be what you looking for.

    Code:
                  int i;
                  
                  i = ( ( i < 0 ) ? ( -1 * i ) : ( i ) );

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by OS_Hacker View Post
    if the question says use bit wise operations then u already have a solution. Well if you dont want to use if..else there is another operator called the conditional operator that ?: ... if the question relates to that then this might just be what you looking for.

    Code:
                  int i;
                  
                  i = ( ( i < 0 ) ? ( -1 * i ) : ( i ) );
    Have you read the OP?
    No if
    No ?:
    No asm
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    May 2007
    Location
    Australia, Melbourne.
    Posts
    5
    Oh i missed that (? in the question
    ... well u definately need to use the bit magic .....

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    And if your machines representation of negative numbers isn't 2's complement, you're hosed anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Actually, inside the computer this:

    1111 1111 is -1 and

    1111 1111 is 255 The way a computer differentiates is by
    checking flags in the FLAG register.

    Just because there's a HIGH bit in the most significant bit doesn't mean
    it's a negative number.

    The JA (jump above) and JG (jump if greater) are coded into the compare
    instruction depending on if the intent was treating the number as signed
    or unsigned.

    Last edited by movl0x1; 05-30-2007 at 06:41 AM.
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  5. Replies: 28
    Last Post: 07-16-2006, 11:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21