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;

}

相关推荐
字节旅行者22 分钟前
C++中如何使用STL中的list定义一个双向链表,并且实现增、删、改、查操作
开发语言·数据结构·c++·链表
搞程序的心海24 分钟前
用Scala玩转Flink:从零构建实时处理系统
开发语言·flink·scala
x66ccff32 分钟前
[特殊字符] Pandas 常用操作对比:Python 运算符 vs Pandas 函数
开发语言·python·pandas
逆风优雅1 小时前
python 爬取网站图片的小demo
开发语言·python
m0_616188491 小时前
PDF预览-搜索并高亮文本
开发语言·javascript·ecmascript
IT瘾君1 小时前
Java基础:Logback日志框架
java·开发语言·logback
stevenzqzq1 小时前
kotlin中主构造函数是什么
开发语言·python·kotlin
Tttian6221 小时前
Python办公自动化(2)对word&pdf的操作
开发语言·python
美美打不死2 小时前
webpack js 逆向 --- 个人记录
开发语言·javascript·webpack
等雨季2 小时前
scala编程语言
开发语言·scala