CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Comparison

  1. #1
    Join Date
    Jun 2003
    Location
    India
    Posts
    118

    Comparison

    Hi Gurus,

    Its somewhat frustrating question.

    Is there any way to compare two numbers without using any conditional operator like (<,>) ?

    thanx,
    -piyu

  2. #2
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399
    why cant you use the '<','>', etc. operators ?
    your humble savant

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396
    What do you mean under "to compare two numbers" ?

  4. #4
    Join Date
    Jun 2003
    Location
    India
    Posts
    118
    See,

    Suppose a = 10 and b = 20. are two numbers.

    i want to know which is greater out of them. For this purpose we can use '<','>' operators. but i don't want to use them to achieve the result as 20.

    Is there any way out ?

  5. #5
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399
    You dont want to use:

    PHP Code:
    int a 10 ;
    int b 20 ;

    if(
    a>b)
    {
      
    // do something
    }
    if(
    b>a)
    {
      
    // do something
    }
    if(
    20==b)
    {
      
    // do something

    ?
    your humble savant

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Yah, inequality operators are terrible for determining order.
    I mean what does one have to do with the other.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  7. #7
    Join Date
    Jun 2003
    Location
    India
    Posts
    118
    yes, TheRogue

    I really don't want to use these operators.

    I am also thinking that we can' t do this.. but still i wan't to raise on CODEGURU coz there are really good ppls who can answer it well.

    -Piyu

  8. #8
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Well, I am just curious. Please tell us why you don't want to use
    them.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  9. #9
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Pretty please..with sugar on top.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  10. #10
    Join Date
    Jun 2003
    Location
    India
    Posts
    118
    Hi souldog,

    Thanx for being curios with the question.

    See.
    I want to write an assembly language code for one specific microprocessor which doesn't contain any cmp ('compare' ), jl('jump if less'), jg (jump if greater) instructions. so i am thinking how it is possible to find greatest number out of A and B.

    May be my approach is wrong. isn't it ?

    please suggest.

    -Piyu

  11. #11
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399
    If it has a check for zero,
    could you decrement both variables until one of them is zero ?
    your humble savant

  12. #12
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Well if you can look at the bits in the numbers starting with
    the MSB and find the first place they are different, then the number
    that has a 1 in this bit is larger.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  13. #13
    Join Date
    Jun 2003
    Location
    India
    Posts
    118
    hi TheRogue


    ---------------
    If it has a check for zero,
    could you decrement both variables until one of them is zero ?
    ----------------

    Fortunatly, i have that instruction with me.
    I can able to decrement both variable until one of them is ZERO.

    now please clarify..

    regards,
    -Piyu

  14. #14
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399
    start:
    decrement both variables

    if neither variable has reached zero, goto start:

    if one variable has reached zero it is smaller

    if both variables have reached zero they are the same



    ps. what is the microprocessor ?
    your humble savant

  15. #15
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Do you have a carry flag in a FLAGS register. You can subtract the two numbers and see if a borrow was needed.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured