bitwise operators for finding less than in c

后端 未结 4 1442
离开以前
离开以前 2020-12-19 03:45

This is a homework assignment which requires me to come up with a function to determine if x < y, if it is I must return 1, using only bitwise o

4条回答
  •  情深已故
    2020-12-19 04:44

    In order to know if x < y, you can simply ask if x - y < 0.

    In other words, what is the sign of the result of x - y.

    Since you stated you are to assume 32 bit integers, following will provide the correct result:

    ((x - y) >> 31) & 0x1
    

提交回复
热议问题