本文介绍了如果测试的整数使用位操作大写ASCII字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关的转让,我试图让一些C code,它仅使用位操作来测试是否一个整数是ASCII大写字母。这封信将其ASCII code以整数形式给出,这意味着的0x41< = ourint< = 5AH即可。做一些研究后,我了解到,小写和大写字母之间的差异是这个词的二进制版本的第六位。大写有一个 1 ,下有一个 0

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 code in integer form, meaning 0x41 <= ourint <= 0x5A. After doing some research I learned that the difference between a lower case and an upper case letter was the sixth digit of the binary version of the word. Uppercase had a 1 and lower had a 0.

我几乎有它,但我的code将无法工作。

I am almost have it, but my code won't work.

我至今是

!(((x>>6)<<31) & 0)

它的作用是转移的code,以获得第六位作为第一数量的权利6,然后向右移动31让它成为全部为0或1,其次是31 0。我必须让这个如果是大写字母则返回1,所以我用感叹号来表示。

What it does is shifts the code to the right 6 to get the sixth digit as the first number, then shifts right 31 to get it to be either all 0's or 1 followed by 31 0's. I have to make it so that if it is uppercase it returns 1, so I use the exclamation point to that.

编辑:我的新的code是

!((~(((x & 32)>>5))<<31))>>31)

但现在我被困在为0x7FFFFFFF

推荐答案

您可以测试是否ASCII字母 C 是大写通过检查为0x20 位,它必须是 0 大写和 1 小写:

You can test if an ASCII letter c is upper case by checking its 0x20 bit, it must be 0 for uppercase and 1 for lowercase:

if (!(c & 0x20))
    printf("ASCII letter %c is uppercase\n", c);

但要注意,如果你不已经知道, C 是一个字母这个测试是行不通的。它会错误匹配'@''[''\\\\']'^''_',并与高位192设置为223个字符,这是不ASCII的一部分,但都是有效的 unsigned char型值的整个范围。

but be aware that this test does not work if you don't already know that c is a letter. It would erroneously match '@' and '[', '\\', ']', '^' and '_', and the whole range of characters with the high bit set from 192 to 223, which are not part of ASCII but are valid unsigned char values.

如果你想有一个单独的测试,如果来验证ç是大写ASCII字母,请尝试:

If you want a single test to verify if c is an uppercase ASCII letter, try:

if ((unsigned)(c - 'A') <= (unsigned)('Z' - 'A'))
     printf("%c is an uppercase ASCII letter\n", c);

编辑:目前还不清楚您的我不允许使​​用if语句,或任何种类的类型转换操作的意思。我必须测试,看看是否数字是两个数字之间,包括远远超出ASCII code的范围内的数字,并返回1,如果是要不0


  • 如果您知道 C 是一个字母,既!(C&安培;为0x20)(((C&GT;&GT; 5)及1)^ 1)将值 1 如果 ç是大写和 0 如果没有

  • 如果 C 可以是任何整数值,只是写正规的比较(C&GT; ='A'和;和C&LT; ='Z'),编译器将通过尝试危险的位变换花样产生更好的code比你。

  • If you know c is a letter, both !(c & 0x20) and (((c >> 5) & 1) ^ 1) will have value 1 if c is uppercase and 0 if not.
  • If c can be any integer value, just write the regular comparison (c >= 'A' && c <= 'Z') and the compiler will produce better code than you would by attempting hazardous bit-twiddling tricks.

再次编辑

由于 C 可以是任何整数值,你只被允许位操作,这里是另一种解决方案:((C&GT;&GT; 5)^ 2)及(0x07fffffeU&GT;&GT;(三及31条))。下面是一个程序来测试这一点:

Since c can be any integer value and you are only allowed bit manipulations, here is another solution: !((c >> 5) ^ 2) & (0x07fffffeU >> (c & 31)). Below is a program to test this:

#include <stdio.h>
#include <stdlib.h>

static int uppertest(int c) {
    return !((c >> 5) ^ 2) & (0x07fffffeU >> (c & 31));
}

int main(int argc, char *argv[]) {
    for (int i = 1; i < argc; i++) {
        int c = strtol(argv[i], NULL, 0);
        printf("uppertest(%d) -> %d\n", c, uppertest(c));
    }
    return 0;
}

这篇关于如果测试的整数使用位操作大写ASCII字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 18:19