2.69-写出具有如下原型的函数的代码:

接题目:

/*
* Do rotating left shift.
Assume0<=n<w
* Examples when x = 0x12345678 and w = 32:
*
n=4 -> 0x23456781, n=20 -> 0x67812345
*/
unsigned rotate_left (unsigned x,int n);

函数应该遵循位级整数编码规则。要注意n=0的情况。

开始作答 官方答案(已验证)

就是要做循环左移,这个n指的是位数。

#include <stdio.h>
#include <assert.h>

/*
* Do rotating left shift.
Assume0<=n<w
* Examples when x = 0x12345678 and w = 32:
*
n=4 -> 0x23456781, n=20 -> 0x67812345
*/
unsigned rotate_left(unsigned x, int n) {
	int w = sizeof(unsigned) << 3;
	/* pay attention when n == 0 */
	return x << n | x >> (w - n - 1) >> 1;
}

int main(int argc, char* argv[]) {
	assert(rotate_left(0x12345678, 4) == 0x23456781); 
	assert(rotate_left(0x12345678, 20) == 0x67812345);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
make: Leaving directory '/home/wanglangtao/frifly/rk3399_linux_release_v2.5.1_20210301/buildroot' 2023-06-07T03:54:43 >>> host-gcc-initial 8.4.0 Building 2023-06-07T03:54:47 >>> host-gcc-initial 8.4.0 Installing to host directory 2023-06-07T03:54:49 >>> host-libtool 2.4.6 Extracting 2023-06-07T03:54:50 >>> host-libtool 2.4.6 Patching 2023-06-07T03:54:50 >>> host-libtool 2.4.6 Updating config.sub and config.guess 2023-06-07T03:54:50 >>> host-libtool 2.4.6 Configuring 2023-06-07T03:54:53 >>> host-libtool 2.4.6 Building 2023-06-07T03:54:54 >>> host-libtool 2.4.6 Installing to host directory 2023-06-07T03:54:55 >>> host-autoconf 2.69 Extracting 2023-06-07T03:54:55 >>> host-autoconf 2.69 Patching 2023-06-07T03:54:55 >>> host-autoconf 2.69 Updating config.sub and config.guess 2023-06-07T03:54:55 >>> host-autoconf 2.69 Patching libtool 2023-06-07T03:54:55 >>> host-autoconf 2.69 Configuring 2023-06-07T03:54:56 >>> host-autoconf 2.69 Building 2023-06-07T03:54:57 >>> host-autoconf 2.69 Installing to host directory 2023-06-07T03:54:57 >>> host-automake 1.15.1 Extracting 2023-06-07T03:54:57 >>> host-automake 1.15.1 Patching 2023-06-07T03:54:57 >>> host-automake 1.15.1 Updating config.sub and config.guess 2023-06-07T03:54:57 >>> host-automake 1.15.1 Patching libtool 2023-06-07T03:54:58 >>> host-automake 1.15.1 Configuring 2023-06-07T03:54:59 >>> host-automake 1.15.1 Building 2023-06-07T03:54:59 >>> host-automake 1.15.1 Installing to host directory 2023-06-07T03:55:00 >>> host-pkgconf 0.9.12 Extracting 2023-06-07T03:55:00 >>> host-pkgconf 0.9.12 Patching 2023-06-07T03:55:00 >>> host-pkgconf 0.9.12 Updating config.sub and config.guess 2023-06-07T03:55:00 >>> host-pkgconf 0.9.12 Patching libtool 2023-06-07T03:55:00 >>> host-pkgconf 0.9.12 Configuring 2023-06-07T03:55:00 >>> host-pkgconf 0.9.12 Building 2023-06-07T03:55:01 >>> host-pkgconf 0.9.12 Installing to host directory 2023-06-07T03:55:02 >>> host-libxml2 2.9.7 Extracting 2023-06-07T03:55:02 >>> host-libxml2 2.9.7 Patching 2023-06-07T03:55:02 >>> host-libxml2 2.9.7 Updating config.sub and config.guess 2023-06-07T03:55:02 >>> host-libxml2 2.9.7 Patching libtool 2023-06-07T03:55:02 >>> host-libxml2 2.9.7 Configuring 2023-06-07T03:55:06 >>> host-libxml2 2.9.7 Building 2023-06-07T03:55:22 >>> host-libxml2 2.9.7 Installing to host directory 2023-06-07T03:55:24 >>> host-gettext 0.19.8.1 Extracting 2023-06-07T03:55:25 >>> host-gettext 0.19.8.1 Patching 2023-06-07T03:55:26 >>> host-gettext 0.19.8.1 Updating config.sub and config.guess 2023-06-07T03:55:26 >>> host-gettext 0.19.8.1 Configuring 2023-06-07T03:55:26 >>> host-gettext 0.19.8.1 Autoreconfiguring 2023-06-07T03:55:51 >>> host-gettext 0.19.8.1 Patching libtool 2023-06-07T03:56:13 >>> host-gettext 0.19.8.1 Building什么问题
06-08

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

origami林

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值