位运算详解与实例

 多字节转换为单字节
#include <stdio.h>

int main()
{
	//int data = 65793;
	int data = 16843009;

	char data_one = data & 0xff;

	data = data>>8;

	char data_two = data & 0xff;

	data = data>>8;

	char data_three = data & 0xff;

	data = data>>8;

	char data_four = data & 0xff;

	printf("%d\n",data_one);

	printf("%d\n",data_two);

	printf("%d\n",data_three);

	printf("%d\n",data_four);

	printf("LengthOfFloat = %d\n",sizeof(float));//4

	printf("LengthOfDouble = %d\n",sizeof(double));//8

	return 0;
}


单字节转换为多字节

#include <stdio.h>

int main()
{
	//int data = 65793;
	int data = 16843011;

	char data_one = data & 0xff;

	data = data>>8;

	char data_two = data & 0xff;

	data = data>>8;

	char data_three = data & 0xff;

	data = data>>8;

	char data_four = data & 0xff;

	printf("%d\n",data_one);

	printf("%d\n",data_two);

	printf("%d\n",data_three);

	printf("%d\n",data_four);

	printf("LengthOfFloat = %d\n",sizeof(float));

	printf("LengthOfDouble = %d\n",sizeof(double));

	data = data_four<<24;

	data += (data_three<<16);

	data += (data_two<<8);

	data += data_one;

	printf("\n%d\n",data);

	return 0;
}


//理解清楚

#include <stdio.h>

int main()
{
	//int data = 65793;
	int data = 16843011;

	char data_one = data & 0xff;

	data = data>>8;

	char data_two = data & 0xff;

	data = data>>8;

	char data_three = data & 0xff;

	data = data>>8;

	char data_four = data & 0xff;

	printf("%d\n",data_one);

	printf("%d\n",data_two);

	printf("%d\n",data_three);

	printf("%d\n",data_four);

	printf("LengthOfFloat = %d\n",sizeof(float));

	printf("LengthOfDouble = %d\n",sizeof(double));

	data = ( (data_four<<24) & 0xff000000 );

	data += ( (data_three<<16) & 0x00ff0000 );

	data += ( (data_two<<8) & 0x0000ff00 );

	data += ( data_one & 0x000000ff );

	printf("\n%d\n",data);

	return 0;
}

 

 

实现a,b相加

#include <stdio.h>


int sum(int a,int b)
{
	if(a == 0)
	{
		return b;
	}
	else
	{
		return sum((a & b) << 1,a ^ b);
	}
}
void main()
{
	printf("sum = %d\n",sum(123,456));
}


///

 #include<stdio.h>
int main()
{
    int  a='abc';
    printf("%s\n",&a);
    //printf(&a);
}

输出:cba
请按任意键继续. . .
为什么会输出cba呢?

int a='abc';
编译器会将'abc'扩展为int,它的值为
6382179 即0x00616263
而intel int内存的实际存储格式是低位在前,所以实际a指向的内容为
0x63626100
如果按字串输出,正好是
cba
且正好是以\0结尾。


你的机器是小端,所以按照从小到大的顺序打印cba;因为你的int是4位,而abc是三位,所以可以正常输入和打印。

说的很透彻了,在little-endian的架构上上面的代码会输出cba,但是在big-endian的机器上就不会这样了。
所以这个题目要区别说明是大端还是在小端的机器上。

在小端的机器上结果是:cba
在大端的机器上结果是:abc


/*
     字节对齐与大小端问题,参考http://blog.csdn.net/reiskie/article/details/1647605
*/

#include<stdio.h>
int main()
{
    int  a='abc';
    int  b=6382179;  //'abc'扩展为int (IDE:VC6.0)
    
    printf("%02x\n",b);  //VC6.0显示616263

    printf("%s\n",&a);
    
}

  #include <stdio.h>
  #include <stdlib.h>
  
  int main(void)
  {
      int a = 'abc';
      char *str = "cba";
      int b;
  
      memcpy(&b, str, 4);
      printf("(char *)&a = %s\n", &a);
      printf("a 的 4 个字节(从高位到低位):%02x %02x %02x %02x\n"
              , (a >> 24) & 0xFF
              , (a >> 16) & 0xFF
              , (a >> 8 ) & 0xFF
              , a & 0xFF);
  
      printf("(char *)&b = %s\n", &b);
      printf("b 的 4 个字节(从高位到低位):%02x %02x %02x %02x\n"
              , (b >> 24) & 0xFF
              , (b >> 16) & 0xFF
              , (b >> 8 ) & 0xFF
              , b & 0xFF);
      return 0;
  }


几个问题:

#include <stdio.h>

int main()
{
	int a = 15,b = 2;

	 a /= b + 1;

	 int c = (1,2);

	printf("%d,%d\n",a,c);//5,2

	return 0;
}



 


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值