Determine the sign of a 32 bit int

后端 未结 8 1521
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 23:05

Using ONLY:

! ~ & ^ | + << >>

NO LOOPS

I need to determine the sign of a 32 bit integer and I need to return 1 if positive, 0 if 0 and -1 if ne

8条回答
  •  囚心锁ツ
    2021-02-08 23:35

    Dimitri's idea could be simplified to (!!x) - ((x >> 30) & 2)

    And just to give one more cryptic solution:

    ~!x & ((-((unsigned) x >> 31)) | !!x)
    

提交回复
热议问题