C Primer Plus(第六版)15.9 编程练习 第5题

//

// main.c

// 15.9-5

//

// Created by cjm on 2024/2/5.

//

#include <stdio.h>

#include <limits.h>

char * itobs(int n, char * ps);

unsigned int rotate(unsigned int x,int i);

int main()

{

unsigned int x=2999999999;

printf("%u\n",rotate(x,3));//注意这里的打印格式

return 0;

}

unsigned int rotate(unsigned int x,int i)

{

unsigned int len;

len = sizeof (unsigned int)*CHAR_BIT;

char str[len+1];

while(i!=0)

{

printf("旋转前 x=%s\n",itobs(x, str));

printf(" x<<1=%s\n",itobs(x<<1, str));

printf("x>>(len-1)=%s\n",itobs(x>>(len-1), str));

x=x<<1|((x>>(len-1))&1);

printf("旋转后 x=%s\n",itobs(x, str));

i--;

}

return x;

}

char * itobs(int n, char * ps)

{

int i;

const static int size = CHAR_BIT * sizeof (int);

for (i = size - 1; i >= 0; i--, n >>= 1)

{

ps[i] = (01 & n) + '0';

}

ps[size] = '\0';

return ps;

}

相关推荐
l1t15 小时前
我改写的二分法XML转CSV文件程序速度追上了张泽鹏先生的
xml·c语言·人工智能·算法·expat
摸鱼仙人~16 小时前
深入理解 MyBatis-Plus 的 `BaseMapper`
java·开发语言·mybatis
wdfk_prog17 小时前
[Linux]学习笔记系列 -- lib/dump_stack.c 栈回溯打印(Stack Trace Dumping) 内核调试与错误诊断的基石
linux·运维·服务器·c语言·笔记·学习
ITKEY_17 小时前
flutter日期选择国际化支持
开发语言·javascript·flutter
刃神太酷啦17 小时前
C++ 异常处理机制:从基础到实践的全面解析----《Hello C++ Wrold!》(20)--(C/C++)
java·c语言·开发语言·c++·qt·算法·leetcode
CYRUS_STUDIO17 小时前
OLLVM 移植 LLVM18 踩坑:一步步调试修复控制流平坦化
c语言·c++·llvm
q5673152317 小时前
告别低效:构建健壮R爬虫的工程思维
开发语言·爬虫·r语言
枫叶丹417 小时前
【Qt开发】显示类控件(一)-> QLabel
开发语言·qt
Python私教17 小时前
源滚滚Rust全栈班v1.02 无符号整数详解
开发语言·后端·rust
yBmZlQzJ18 小时前
PyQt5 修改标签字体和颜色的程序
开发语言·python·qt