size_t get_nl(char *sfrom)
{
unsigned char *from = (unsigned char *)sfrom;
size_t t;
t = (*from++) << 24;
t |= (*from++) << 16;
t |= (*from++) << 8;
t |= *from;
return t;
}
并且希望能告诉我| =是什么作用,先谢了!!
unsigned char *from = (unsigned char *)sfrom;/*强制类型转换*/
size_t t;/*如果没猜错的话,size_t应该是自定义类型,typedef char size_t;*/
/* (*from++)相当于先把from[0]<<24(向左移24位,由于from[0]是8位,
所以向左移24后from[0]的值为0,再赋给t(t=0)),再使from指向from[1] */
t = (*from++) << 24
/* t|=var;相当于t=t|var; */
/*(*from++)相当于先把from[1]<<16(向左移16位,由于from[1]是8位,
所以向左移16后from[1]的值为0,将t|=0相当于t=t|0;
由上一语句可知t=0,再使from指向from[2] */
t |= (*from++) << 16;
/*(*from++)相当于先把from[2]<<8(向左移8位,由于from[2]是8位,
所以向左移8后from[2]的值为0,将t|=0相当于t=t|0;
由上一语句可知t=0,再使from指向from[3] */
t |= (*from++) << 8;
/*t|=*form;相当于t=t|from[3]; 由上一语句可知t=0,所以结果t的值为from[3]*/
t |= *from;
return t;
/*t的值为from[3]*/
我测试了一下:
#include<stdio.h>
#include<conio.h>
main()
{
char *str="zhaoxufeng";
char t;
clrscr();
t=(*str++)<<24;
t|=(*str++)<<16;
t|=(*str++)<<8;
t|=*str;
printf("%c",t);
getch();
}
结果:
o