首页 > 其他 > 详细

获取内存中整数二进制形式

时间:2015-03-28 14:20:17      阅读:176      评论:0      收藏:0      [点我收藏+]

采用移位和相与方式求整数在内存中的二进制形式。

#include<stdio.h>
typedef  int DataType;
int num_covert_binary(DataType num);

void main()
{
	DataType num;
	num = -1;
	num_covert_binary(num);
	num = 12;
	num_covert_binary(num);
	getchar();//让console 等待一下
}
int num_covert_binary(DataType num)
{
	DataType base_data;
	base_data = 1;
	base_data = base_data << (sizeof(DataType)* 8 - 1);//相当最高位是1其它位为0
	printf("\n%d在内存中二进制形式为:\n", num);
	for (int i = 1; i <= sizeof(DataType)* 8; i++)
	{
		DataType temp = base_data#

		if (temp == 0) //相与的结果为 0
		{
			putchar('0');
		}
		else
		{
			putchar('1');
		}
		if (i % 4 == 0)
		{
			printf(" ");
		}
		num = num << 1; //左移一位
	}
	return 0;

}

技术分享


获取内存中整数二进制形式

原文:http://blog.csdn.net/huangshanchun/article/details/44701043

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!