bitwise isgreater help

Posted on 2009-04-16
Last Modified: 2013-11-25
i can't seem to get this to work.  It always returns the opposite of what i want.. ideas?

(i'd only like to use bitwise operators)

 * isGreater - if x > y  then return 1, else return 0


int isGreater(int x, int y) {

int val1 = !(x >> 31) & (y >> 31);

int val2 = !(x >> 31 ^ y >> 31);

int val3 = ((y + x + 1) >> 31);

int val4 = (val1 | val2) & val3;

return (!!(val4^0));



Open in new window

Question by:braker15
    LVL 30

    Accepted Solution

    Hi braker15,

    I didn't really analyze the function, but this IMO is senseless:
    > return (!!(val4^0));

    Here you use the !-operator two times which is the same as if you didn't use it at all.

    LVL 53

    Assisted Solution

    Using signed ints with shift operators (more specifically right shift operators) is not recommended, unless you know how they work. Use unsigned ints instead.

    I take it you verified that on your system, an int is 32 bits wide, has 31 value bits, and 1 sign bit ? If not, you should verify that, and mark the code as platform-dependent.

    >> (i'd only like to use bitwise operators)

    You're using arithmetic operators (+) and boolean operators (!) too ... Is that ok ?

    >>  It always returns the opposite of what i want..

    Well, take x = 2 and y = 1, and see what each of the temporary values is set to ... Is that what you intended ? Can you see where it goes wrong ?
    LVL 53

    Expert Comment

    May I ask why you gave a B grade ? That usually means that something was missing in the answer and/or that something is still unclear. If that's the case, then please do not hesitate to ask for clarification where needed.
    LVL 1

    Author Comment


    because I still never got a reason for why the result was always the opposite of what I wanted.
    LVL 53

    Expert Comment

    >> because I still never got a reason for why the result was always the opposite of what I wanted.

    Why didn't you ask about that then ? We can't know what more information you still need, if you don't tell us.

    The reason for why it doesn't give you the result you expect, is because the code does something else :) If you go through the code using the example I posted, it should be clear.
    You need to fix the code to make it do what you want.

    Did you also take into account all of the advice given ?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now