dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 56

Thread: Implementing If-then-else without branching

  1. #1
    Join Date
    Feb 2004
    Posts
    3

    Implementing If-then-else without branching

    How can the if-then-else clause be implmented without branching. Only operators allowed are ~, !, ^, &, +, |, <<, >>.

    For eg:
    Code:
    int Compare( int x, int y, int z )
    {
    	if ( x == 0 )
    		return y;
    	else
    		return z;
    }

    Thanks

  2. #2
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567

    Implementing If-then-else without branching

    If you could use a ternary operator then :

    Code:
    int w= (x==0) ? y :  z;
    Code:
    This uses branching but just for comparison
    
    int Compare( int x, int y, int z )
    {
        return  ( x == 0 ) ? y : z;
    }
    But "? and :" were not included in your list of possible operators.

    TDM
    Last edited by TDM; February 9th, 2004 at 05:58 AM.

  3. #3
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    That sounds like a homework type of question. We avoid providing direct answers to homework and class assignements and things like that.

    Is that the entire question? My guess is that it is not. Perhaps you don't understand the question and therefore don't realize that we need more of the question. If you can provide the entire question, then we can at least help you understand the question.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  4. #4
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    712
    Code:
    int compare(int x, int y, int z)
    {
    	int a[]={z, y};
    	return a[x==0];
    }
    Have fun...
    Popular opinion is the greatest lie in the world.

  5. #5
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    Nice,

    Don't want to open a can of worms here and get too picky.

    However, as long as x and y are quantities known at run-time instead of compile-time, the compiler must always generate a compare and branch sequence at the assembler level no matter what language artifacts attempt to hide this?

    I consider the assignment to be poorly formulated, incomplete or both. Furthermore, it can be down right misleading to the learning programmer to sort of create the mirage that you can get something for nothing out of a compiler.

    Oh well whatever.

    BTW, I think the answer from AvDav is kind of cute...

    Sincerely, Chris.

    You're gonna go blind staring into that box all day.

  6. #6
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by dude_1967
    Furthermore, it can be down right misleading to the learning programmer to sort of create the mirage that you can get something for nothing out of a compiler.
    It is probably the instructor using the term "branching" and if si then it is the instructor that is confusing the student.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  7. #7
    Join Date
    Nov 2000
    Posts
    66
    Okkk everybody, sorry for not being able to post early on.

    This is not an assignment problem. I am not a student. I am already holding a master's degree in computer science.

    Okk now for someone who wants to know why this problem. It was one of the problem asked to me on an interview and I couldn't come up with a satisfactory answer. So, its here for more people to put in their views.

    Also, the question is complete and that is what was given to me. Again, using a temporary variable is not a solution like AvDav suggested.

    For TDM:
    The problem is to implement the ternary operator without branching. So obviously use of ternary operator or in general any operator other than the ones on the question is not allowed.

    My guess is that the answer has something to do with the & and | operators. But not sure. It shouldn't be a very though one for somebody writing code in assembly or for embedded systems.

    Thanks for all your input.

  8. #8
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Well we need to have definitions of branching and/or have the scope defined in terms of language. C and C++ does not have anything called "branching".
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  9. #9
    Join Date
    Nov 2000
    Posts
    66
    This is how the question was put to me.

    An if-then-else when written in assembly will generate a jump operation. Write the if-then-else clause so that you don't have a jump statement. And the question was followed with what is given on the first post.

    Now just for an example:

    Code:
    if x, y being two integers
    
    if ( x == 0 )
         y = 0;
    else
         y = 1;
    
    can be written simply as
    
    y = !!x;
    This was another one on the test.

  10. #10
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    How about this:
    Code:
    int Compare(int x, int y, int z)
    {
        return (y<<(!x)) + (z << (!!x)) + ~y + 1 + ~z + 1;
    }

  11. #11
    Join Date
    Nov 2000
    Posts
    66
    Really nice j0nas

    That's exactly what I was looking for.

    Thanks

  12. #12
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by Hitesh1903
    Really nice j0nas

    That's exactly what I was looking for.

    Thanks
    It is very important to be clear about what you ask for, since you asked for "the if-then-else clause" as an answer. I see no if-then-else clause in the answer you say you were looking for. It seems you meant to ask for the result of the specified if-then-else statement except without an if-then-else or something like that.

    I think if a prospective employer were to ask the question as it was initially asked, they would get plenty of complaints.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  13. #13
    Join Date
    Nov 2000
    Posts
    66
    Sorry Sam but I haven't rephrased the question. I posted the one that was given to me. What I posted here were the words of the interviewer. He presented the question the same way I presented here.

    Still, I shall keep your suggestions in mind for my future postings.

    And Yes Sir, just FYI that question was from a M$ interviewer.

  14. #14
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Originally posted by j0nas
    How about this:
    Code:
    int Compare(int x, int y, int z)
    {
        return (y<<(!x)) + (z << (!!x)) + ~y + 1 + ~z + 1;
    }
    Great work j0nas!

    I am trying understand if there is any pratical value for doing that. Is there any reason to avoid branching? It seems to me that it only obsfucates the code. In my opinion, it is bad for production code, especially for future maintainence.

  15. #15
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    If the initial question is a valid question, then I apologize for all my criticisms of it.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

Page 1 of 4 1234 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)