1) int isAsciiDigit(int x); 功能:当0x30<=x<=0x39时(即字符0-9的ASCII码值)返回1;其他情况下返回0 示例: isAsciiDigit(0x35) == 1 isAsciiDigit(0x3a) == 0 isAsciiDigit(0x05) == 0 难度:3 可使用运算符数:15
时间: 2023-05-28 17:08:09 浏览: 101
int isAsciiDigit(int x) {
int mask = 1 << 31; // mask = 0x80000000
int upperBound = ~(mask >> 4); // upperBound = 0x39
int lowerBound = ~(mask >> 3); // lowerBound = 0x30
int diff1 = x - upperBound;
int diff2 = lowerBound - x;
int sign1 = diff1 >> 31; // sign1 = 0xFFFFFFFF if x > upperBound, 0 otherwise
int sign2 = diff2 >> 31; // sign2 = 0xFFFFFFFF if x < lowerBound, 0 otherwise
return !(sign1 | sign2); // return 1 if both signs are 0, 0 otherwise
}
// 使用了位运算符:<<, >>, |, ~, &
相关问题
c语言将uint8_t a = 0x01转换为 ASCII码uint8_t b = 0x31
要将 uint8_t 类型的值转换为对应的 ASCII 码,可以使用类型转换操作符将其转换为 char 类型。例如,将 uint8_t 类型的变量 `a` 的值 0x01 转换为 ASCII 码,并将结果存储在 uint8_t 类型的变量 `b` 中,可以使用以下代码:
```c
#include <stdio.h>
#include <stdint.h>
int main() {
uint8_t a = 0x01;
uint8_t b = static_cast<uint8_t>(a + '0');
printf("ASCII码为:%c\n", b);
return 0;
}
```
上述代码将输出字符 '1',其对应的 ASCII 码是 0x31。
注意,这里使用了字符 '0' 的 ASCII 码值(0x30)作为基础,通过加上 `a` 的值得到对应的 ASCII 码。因为 0x01 加上 '0' 的 ASCII 码值等于 0x31,所以最终结果是字符 '1' 的 ASCII 码值。
请注意,在 C 语言中,类型转换操作符可以用于显式转换不同类型的值。在上述示例中,使用 `static_cast` 进行类型转换。
用下列语句定义a,b,c,然后执行b=a、c='b'+b,则b,c的值是(). long a=0xffffff; int b; char c; A:0ffffff 和0x61 B:-1和98 C:-1和97 D:指向同一地址
根据题意,执行 `b=a` 后,`b` 的值为 `0xffffff`,因为 `long` 类型的 `a` 的值为 `0xffffff`,也就是二进制的 `11111111 11111111 11111111`。
执行 `c='b'+b` 时,需要注意 `'b'` 的 ASCII 码值为 `98`,而 `b` 的值为 `0xffffff`,将它们相加得到 `98+0xffffff=1000001`,根据 ASCII 码表可知 `1000001` 对应的字符是 `'A'`,因此 `c` 的值为 `'A'`。
所以,选项为 C:`-1` 和 `97`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)