Thread: bitwise Absolute value?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    58

    bitwise Absolute value?

    I have a small issue that I can't seem to figure out, so I thought that I would run it by all of you.

    The highest order bit in a byte has a value of 128.
    From my understanding, signed numeric data types use this bit to tell the number if it is positive or negative.

    In an int, there are 4 bytes, which multiplied by 8, gives you how many bits there are in an int.

    well...if you shift 128 to the left by the number of bytes in an int, and subtract 1, then multiply by 8, wouldn't you have the highest order bit of an integer?
    Example: ( 128 << ((sizeof(int) - 1) * 8) ) : (128 << 24)

    I also tried (128 << ((sizeof(int)) * 8)) : (128 << 32)
    in order to isolate the bit, just in case my reasoning was flawed.

    In order to obtain the absolute value though, I took that bit, and ANDed it with the value of my int, and then XORed it, to try and obtain the absolute value....
    Example:


    Code:
    byte value = -2;
    (1000 0010)

    Code:
    byte highbit = (128 << ((sizeof(byte) - 1) * 8));
    128 or -0

    Code:
    byte valueHighbit = highbit & value;
    1000 0010 //value
    1000 0000 //highbit
    -----------------------
    1000 0000 //tells us the highbit is on in the value


    Code:
    byte result = valueHighbit ^ value;
    1000 0000 //valueHighbit;
    1000 0010 //value;
    ------------------------------
    0000 0010 //result

    So as you can see, the result should be positive 2.

    However, if you try this, you will see it is not...Why is this?
    Why does this particular approach not work? Is it the way in which I am trying to obtain the highest bit? I can do this with various other methods, but I wanted to know why this one appears to be incorrect. Please educate me!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    My calculator shows that -2 is not 1000 0010, but 1111 1110
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by arcaine01
    The highest order bit in a byte has a value of 128.
    From my understanding, signed numeric data types use this bit to tell the number if it is positive or negative.
    Let us assume 8 bit bytes and a two's complement representation for signed integers. You would then be correct to say that that bit has a value of 128 for an unsigned char, but a value of -256 for a signed char.
    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

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Read about two's compliment encoding, which is what is used to represent signed numbers almost everywhere.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    (~x)+1;

    i'm not sure anyone uses the sign-and-magnitude numerical model. as kingmir said, 2's compliment is pretty standard.
    Last edited by m37h0d; 07-30-2009 at 02:30 PM.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    I have used the two's compliment method for getting the absolute value, but I wanted to try other things just so I could have a better understanding of the language, and base two math.

    Now, from my understanding, the two's-compliment standard signed integer type reads
    1111 1111 1111 1111 1111 1111 1111 1110
    as -2.

    which means:
    1111 1111 1111 1111 1111 1111 1000 0000
    is -128


    if I apply the same logic above, using -128, rather than 128, why would it fail?

    Can someone please attempt to correct my flawed logic?

    New understanding is that
    Code:
     (((-128 << ((sizeof(value)-1)>>3))&value)^value)
    should work...

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you even think about what you posted? A little bit? After all, (sizeof(value)-1) >>3 is guaranteed to be zero for just about any data type you care to name (unless you've got a 9-byte-or-larger data type hanging around somewhere). So you've got (-128&value)^value, which isn't going to get you what you want.

    If you've grasped two's complement, then you must know that finding the negative of a number must be done either by (~x)+1 or by something that simplifies to that expression. If it doesn't simplify to that expression, then quite simply it cannot be what you want.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by King Mir
    Read about two's compliment encoding
    Two's complement. Rather strangely, both m37h0d and arcaine01 chose to copy your typographical error
    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 Questions
    By someprogr in forum C Programming
    Replies: 8
    Last Post: 12-14-2008, 06:45 PM
  2. Absolute value of each element in a given array
    By DriftinSW20 in forum C Programming
    Replies: 9
    Last Post: 11-15-2007, 04:08 PM
  3. bitwise operations with double
    By henry_kay in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 04:57 AM
  4. Characters into bitwise ints
    By Code Zer0 in forum C++ Programming
    Replies: 9
    Last Post: 04-24-2003, 08:34 AM