문제

Here's the question

  • sign - return 1 if positive, 0 if zero, and -1 if negative
  • Legal ops: ! ~ & ^ | + << >>
  • Max ops: 10
  • Rating: 2

I thought of shifting to the right by 31 to grab the sign bit but can't 0 be positive AND negative technically?

도움이 되었습니까?

해결책

You may try like this for 32 bit int:-

(x >> 31) | (((~x + 1) >> 31) & 1)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top