《明解C语言》笔记及课后习题答案 【第七章】

练习7-2

/*编写一个程序,确认只要没有发生高位溢出,则:
无符号整数位左移后的值等于其乘以2的指数幂后的值。
无符号整数位右移后的值等于其除以2的指数幂后的值。*/

#include <stdio.h>
#include <limits.h>

int main(void)
{
	unsigned uns_int;
	
	puts("请输入一个整数:");
	
	do{
		scanf("%u",&uns_int);
		if(uns_int > UINT_MAX)
			printf("您输入的整型过大,请重新输入。");
	} while (uns_int > UINT_MAX);
	
	printf("把整数向左移动3位后其值等于其乘以2的指数幂后的值:%u = %u\n",uns_int << 3, uns_int*2*2*2); 
	printf("把整数向右移动3位后其值等于其乘以2的指数幂后的值:%u = %u",uns_int >> 3, uns_int/2/2/2); 
	
	return 0;
 } 

练习7-3

/*---编写rrotate函数,返回无符号整数x右移n位后的值。
编写lrotate函数,返回无符号整数x左移n位后的值。---*/

#include <stdio.h>

unsigned rrotate(unsigned x, int n){
	return x >> n;
}

unsigned lrotate(unsigned x, int n){
	return x << n;
}

int main (void){
	unsigned uns_int;
	int move_bits;
	
	printf("请输入整数和移动位数:");
	scanf("%d %d",&uns_int, &move_bits);
	
	printf("%u向右移%d次后的值:%u\n",uns_int, move_bits, rrotate(uns_int, move_bits)); 
	printf("%u向左移%d次后的值&
  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值