comparison

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ndun78@gmail.com

    #1

    comparison

    I want to write something like
    status_bit = (unsigned)(i - j) >31

    as an equivalent code for
    if(i < j){
    {
    status_bit = 1;
    }else{
    status_bit = 0;
    }

    This actually works for most of the cases but fails for two values
    i = 0x80000000
    j = 0x7fffffff

    I want a linear code for the above i.e. without jumps, so
    1. what could be the issue?
    2. what could be the solution?

  • ndun78@gmail.com

    #2
    Re: comparison

    On Oct 21, 3:10 pm, ndu...@gmail.co m wrote:
    I want to write something like
    status_bit = (unsigned)(i - j) >31
    >
    as an equivalent code for
    signed int i;
    signed int j;
    if(i < j){
    {
    status_bit = 1;
    >
    }else{
    status_bit = 0;
    }
    >
    This actually works for most of the cases but fails for two values
    i = 0x80000000
    j = 0x7fffffff
    >
    I want a linear code for the above i.e. without jumps, so
    1. what could be the issue?
    2. what could be the solution?

    Comment

    • Martin Ambuhl

      #3
      Re: comparison

      ndun78@gmail.co m wrote:
      I want to write something like
      status_bit = (unsigned)(i - j) >31
      >
      as an equivalent code for
      if(i < j){
      {
      status_bit = 1;
      }else{
      status_bit = 0;
      }
      Try the simple
      status_bit = (i < j);
      This actually works for most of the cases but fails for two values
      i = 0x80000000
      j = 0x7fffffff
      >
      I want a linear code for the above i.e. without jumps, so
      1. what could be the issue?
      2. what could be the solution?
      Is there a reason for avoiding the obvious
      status_bit = (i < j);
      ?


      Comment

      • Charlie Gordon

        #4
        Re: comparison

        "Martin Ambuhl" <mambuhl@earthl ink.neta écrit dans le message de news:
        5o0ngiFkk1juU1@ mid.individual. net...
        ndun78@gmail.co m wrote:
        >I want to write something like
        >status_bit = (unsigned)(i - j) >31
        >>
        >as an equivalent code for
        >if(i < j){
        >{
        > status_bit = 1;
        >}else{
        > status_bit = 0;
        >}
        >
        Try the simple
        status_bit = (i < j);
        >
        >This actually works for most of the cases but fails for two values
        >i = 0x80000000
        >j = 0x7fffffff
        >>
        >I want a linear code for the above i.e. without jumps, so
        >1. what could be the issue?
        >2. what could be the solution?
        >
        Is there a reason for avoiding the obvious
        status_bit = (i < j);
        In other words: if there is a way to compute this without tests and jumps,
        the compiler should know.

        --
        Chqrlie.


        Comment

        • ndun78@gmail.com

          #5
          Re: comparison

          Is there a reason for avoiding the obvious
          status_bit = (i < j);
          >
          I think that it is not guaranteed to get only 1 or 0 as a result of i
          < j, this may result in 0 or non zero value is it not?

          Comment

          • santosh

            #6
            Re: comparison

            ndun78@gmail.co m wrote:
            >
            Is there a reason for avoiding the obvious
            status_bit = (i < j);
            >>
            I think that it is not guaranteed to get only 1 or 0 as a result of i
            < j, this may result in 0 or non zero value is it not?
            No, the relational operators yield either 1 or 0.

            Comment

            • ndun78@gmail.com

              #7
              Re: comparison

              No, the relational operators yield either 1 or 0.
              Is it guaranteed by ANSI C standard?


              Comment

              • pete

                #8
                Re: comparison

                ndun78@gmail.co m wrote:
                >
                Is there a reason for avoiding the obvious
                status_bit = (i < j);
                I think that it is not guaranteed to get only 1 or 0 as a result of i
                < j, this may result in 0 or non zero value is it not?
                No.
                (i < j) is can only be equal to either one or zero.

                --
                pete

                Comment

                • santosh

                  #9
                  Re: comparison

                  ndun78@gmail.co m wrote:
                  >
                  >No, the relational operators yield either 1 or 0.
                  Is it guaranteed by ANSI C standard?
                  Yes. From n1256.pdf:

                  6.5.8 Relational operators

                  ....

                  6 Each of the operators < (less than), (greater than), <= (less
                  than or equal to), and >= (greater than or equal to) shall yield 1
                  if the speci?ed relation is true and 0 if it is false.92)
                  The result has type int.

                  Comment

                  • Army1987

                    #10
                    Re: comparison

                    On Sun, 21 Oct 2007 10:10:51 +0000, ndun78 wrote:
                    I want to write something like
                    status_bit = (unsigned)(i - j) >31
                    >
                    as an equivalent code for
                    if(i < j){
                    {
                    status_bit = 1;
                    }else{
                    status_bit = 0;
                    }
                    >
                    This actually works for most of the cases but fails for two values
                    i = 0x80000000
                    j = 0x7fffffff
                    >
                    I want a linear code for the above i.e. without jumps, so
                    1. what could be the issue?
                    If *you* don't know that...
                    2. what could be the solution?
                    status_bit = (i < j);
                    Boolean operators evaluate to 0 if false and 1 if true.

                    --
                    Army1987 (Replace "NOSPAM" with "email")
                    A hamburger is better than nothing.
                    Nothing is better than eternal happiness.
                    Therefore, a hamburger is better than eternal happiness.

                    Comment

                    • abhy

                      #11
                      Re: comparison

                      On Oct 21, 3:10 pm, ndu...@gmail.co m wrote:
                      I want to write something like
                      status_bit = (unsigned)(i - j) >31
                      >
                      as an equivalent code for
                      if(i < j){
                      {
                      status_bit = 1;
                      >
                      }else{
                      status_bit = 0;
                      }
                      >
                      This actually works for most of the cases but fails for two values
                      i = 0x80000000
                      j = 0x7fffffff
                      >
                      I want a linear code for the above i.e. without jumps, so
                      1. what could be the issue?
                      2. what could be the solution?
                      you as well write this as status_bit = i - j 1 ? 0 : 1

                      Comment

                      • Martin Ambuhl

                        #12
                        Re: comparison

                        ndun78@gmail.co m wrote:
                        >>Is there a reason for avoiding the obvious
                        >> status_bit = (i < j);
                        I think that it is not guaranteed to get only 1 or 0 as a result of i
                        < j, this may result in 0 or non zero value is it not?
                        >
                        You are wrong. Check your elementary C textbook.

                        Comment

                        • Charlie Gordon

                          #13
                          Re: comparison

                          "abhy" <abhijitkrao283 @gmail.coma écrit dans le message de news:
                          1192982476.6191 73.30800@z24g20 00...legro ups.com...
                          On Oct 21, 3:10 pm, ndu...@gmail.co m wrote:
                          >I want to write something like
                          >status_bit = (unsigned)(i - j) >31
                          >>
                          >as an equivalent code for
                          >if(i < j){
                          >{
                          > status_bit = 1;
                          >>
                          >}else{
                          > status_bit = 0;
                          >}
                          >>
                          >This actually works for most of the cases but fails for two values
                          >i = 0x80000000
                          >j = 0x7fffffff
                          >>
                          >I want a linear code for the above i.e. without jumps, so
                          >1. what could be the issue?
                          >2. what could be the solution?
                          >
                          you as well write this as status_bit = i - j 1 ? 0 : 1
                          No, it does not work at all: the expression i - j 1 is not equivalent to
                          !(i < j) and even the more accurate i - j < 0 does not produce the correct
                          result if i - j overflows (as the OP correctly pointed out).

                          If you *really* want to compute the status bit with plain arithmetics then
                          you can use this:

                          status_bit = ((unsigned long long)((long long)i - j) >(CHAR_BIT *
                          sizeof(int) + 1)) & 1;

                          It should work if sizeof(long long) sizeof(int).
                          It may even be acceptably efficient on 64 bit architectures.

                          But it would be crazy to use such a convoluted expression when ``status_bit
                          = (i < j)'' is vastly more readable and quite unlikely to be a performance
                          killer.

                          --
                          Chqrlie



                          Comment

                          • Ben Pfaff

                            #14
                            Re: comparison

                            ndun78@gmail.co m writes:
                            I want to write something like
                            status_bit = (unsigned)(i - j) >31
                            >
                            as an equivalent code for
                            if(i < j){
                            You should buy a copy of the book _Hacker's Delight_ by Henry
                            S. Warren. It is a compendium of tricks like this. This
                            particular problem, and related inequalities, takes up a few
                            pages in chapter 2, for example.
                            --
                            Ben Pfaff

                            Comment

                            • Charlie Gordon

                              #15
                              Re: comparison

                              "Ben Pfaff" <blp@cs.stanfor d.edua écrit dans le message de news:
                              87lk9wm9d2.fsf@ blp.benpfaff.or g...
                              ndun78@gmail.co m writes:
                              >
                              >I want to write something like
                              >status_bit = (unsigned)(i - j) >31
                              >>
                              >as an equivalent code for
                              >if(i < j){
                              >
                              You should buy a copy of the book _Hacker's Delight_ by Henry
                              S. Warren. It is a compendium of tricks like this. This
                              particular problem, and related inequalities, takes up a few
                              pages in chapter 2, for example.
                              You will find this "Bit Twiddling Hacks" entertaining as well:



                              Good night!

                              --
                              Chqrlie.


                              Comment

                              Working...