[英]How this function f1() will execute?
#include <stdio.h>
void f1(char* str, int index)
{
*(str + index) &= ~32;
}
int main()
{
char arr[] = "gatecsit";
f1(arr, 0);
printf("%s", arr);
return 0;
}
How is function f1() working? 函数f1()如何工作?
Being specific *(str + index) &= ~32; 是特定的*(str + index)&=〜32; this one.... thanks
这个...。谢谢
I think f1() capitalizes the first letter of the string by exploiting a property of ASCII that means that corresponding lower and upper-case letters differ by 32. For example the code for 'A' is 65, while that for 'a' is 97. The '&= ~32' bit of the code will turn of bit-5 of the ASCII representation of the character str[index], which should turn the 'g' into 'G'. 我认为f1()通过利用ASCII的属性将字符串的第一个字母大写,这意味着相应的小写和大写字母相差32。例如,“ A”的代码为65,而“ a”的代码为97.该代码的“&=〜32”位将把字符str [index]的ASCII表示的第5位变为“ g”。 This should be fine for strings that contain only ordinary letters, but will have strange effects on digits and punctuation characters.
这对于只包含普通字母的字符串应该是正确的,但是会对数字和标点符号产生奇怪的影响。
The expression 表达方式
*(str + index)
is equivalent to 相当于
str[index]
So the character at position index
is changed the following way 因此,位置
index
处的字符按以下方式更改
*(str + index) &= ~32;
In the ASCII table lower case letters differ from upper case letters by having one more set bit. 在ASCII表中,小写字母与大写字母的不同之处是多了一个设置位。 For example the lower case letter
'a'
has the code in hex 61
while the upper case letter 'A"
has the code in hex 41
. So the difference is equal to the value in hex 20
that in decimal is equal to 32
. 例如,小写字母
'a'
具有十六进制61
的代码,而大写字母'A"
具有十六进制41
的代码,因此,该差等于十六进制20
中的值,十进制等于32
。
So the original expression resets the corresponding bit in the character to 0 converting a lower case letter to the upper case letter. 因此,原始表达式会将字符中的相应位重置为0,从而将小写字母转换为大写字母。
The code removes 1 bit from the character effectively subtracting 32 from the byte or 0x20. 该代码从字符中删除了1位,从字节或0x20有效减去32。
#include <stdio.h>
#include <string.h>
void f1(char* str, int index)
{
// The code removes 1 bit from the character at the position `str[index]`
// effectively subtracting 32 from that character
// Capital letters in ASCII are apart by 32 (0x20) from small letters
// Since 'a' = 0x61 and 'A' = 0x41 'a' - 32 = 'A'
// Since 'b' = 0x62 and 'B' = 0x42 'b' - 32 = 'B'
// `~` is a binary negation operator 0 -> 1; 1 -> 0
// `&` is a binary AND
// x &= y; is equivalent to x = x & y;
// ~0x20 = 0b11011111
*(str + index) &= ~0x20; // 0x20 = 32;
}
int main()
{
int i;
char arr[] = "gatecsit";
size_t len = strlen(arr);
for(i = 0; i< len; i++)
printf(" %c " , arr[i]);
printf("\n");
for(i = 0; i< len; i++)
printf(" %X" , arr[i]);
printf("\n");
// convert all letters:
for(i = 0; i< len; i++)
f1(arr, i);
printf("\n");
for(i = 0; i< len; i++)
printf(" %c " , arr[i]);
printf("\n");
for(i = 0; i< len; i++)
printf(" %X" , arr[i]);
return 0;
}
Output: The small and capital are letters apart by 0x20 (or 32 decimal). 输出:小写字母和大写字母相隔0x20(或32个十进制)。 This can be clearly seen from this printout:
从此打印输出可以清楚地看到:
g a t e c s i t
67 61 74 65 63 73 69 74
G A T E C S I T
47 41 54 45 43 53 49 54
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.