Testing if an integer is an uppercase ASCII letter using bit manipulation

后端 未结 4 734
南方客
南方客 2021-01-28 11:35

For an assignment, I\'m trying to make some code in C that uses only bit manipulation to test if an integer is an ASCII uppercase letter. The letter will be given by its ASCII c

4条回答
  •  感情败类
    2021-01-28 12:09

    Since OP is stuck on case 0x7fffffff, exclude it by extending the otherwise working solution.

    !((~(((x & 32)>>5))<<31))>>31) & !(x ^ 0x7fffffff)
    

    Pedantically, just code as below and let the compiler simplify.

    isupper = (!(x ^ 'A')) | (!(x ^ 'B')) | (!(x ^ 'C')) ... (!(x ^ 'Z'));
    

提交回复
热议问题