Determine the sign of a 32 bit int

后端 未结 8 1634
隐瞒了意图╮
隐瞒了意图╮ 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:22

    If conditionals (not if statements) and subtraction are allowed, the simplest & cleaner solution (IMO) is:

    int sign = (v > 0) - (v < 0);
    

    Not using subtraction (and assuming int is 32 bits):

    #include 
    #include 
    #include 
    
    int process(int v) {
        int is_negative = (unsigned int)v >> 31; // or sizeof(int) * CHAR_BIT - 1
        int is_zero = !v;
        int is_positive = !is_negative & !is_zero;
        int sign = (is_positive + ~is_negative) + 1;
        return sign;
    }
    
    int main() {
        assert(process(0) == 0);
        printf("passed the zero test\n");
        for (int v = INT_MIN; v < 0; v++) {
            assert(process(v) == -1);
        }
        printf("passed all negative tests\n");
        for (int v = 1; v < INT_MAX; v++) {
            assert(process(v) == +1);
        }
        printf("passed all positive tests\n");
        return 0;
    }
    

    Here's are the results:

    $ gcc -o test test.c -Wall -Wextra -O3 -std=c99 && ./test && echo $#
    passed zero test
    passed all negative tests
    passed all positive tests
    0
    

提交回复
热议问题