求任意长度连续数据在内存中的二进制表示(c语言)

参考:<<深入理解计算机系统>>
如下:

#include <stdio.h>
//参数1:数据首地址
//参数2:数据大小(字节数)
//返回值: 空
void funPrintBit(void * pFirstAddress, int iByteNum) //小端法,即低位在前,高位在后
{	
	char *p = (char *)pFirstAddress;				//对小端法来讲,逻辑左移即物理右移
	unsigned char c;								//对小端法来讲,逻辑右移即物理左移
	for (int i = 0; i < iByteNum; i++)
	{
		c = 1;
		for (int j = 0; j < 8; j++)
		{
			printf("%d", (p[i]&c) >> j);
			c = c << 1;
		}
		printf(" ");
	}
	printf("\n");
}

posted @ 2022-04-19 18:17  enbug  阅读(20)  评论(0)    收藏  举报