C 练习实例 54

C 语言教程 · 2019-02-22 07:26:38

题目:取一个整数 a 从右端开始的 4~7 位。

程序分析:可以这样考虑:

(1)先使 a 右移 4 位。

(2)设置一个低 4 位全为 1,其余全为 0 的数,可用~(~0<<4)

(3)将上面二者进行 & 运算。

实例

// Created by www.codercto.com on 15/11/9. // Copyright © 2015年 码农教程. All rights reserved. // #include <stdio.h> int main() { unsigned a,b,c,d; printf("请输入整数:\n"); scanf("%o",&a); b=a>>4; c=~(~0<<4); d=b&c; printf("%o\n%o\n",a,d); return 0; }

以上实例输出结果为:

请输入整数:
36
36
1

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

A Common-Sense Guide to Data Structures and Algorithms

A Common-Sense Guide to Data Structures and Algorithms

Jay Wengrow / Pragmatic Bookshelf / 2017-8-13 / USD 45.95

If you last saw algorithms in a university course or at a job interview, you’re missing out on what they can do for your code. Learn different sorting and searching techniques, and when to use each. F......一起来看看 《A Common-Sense Guide to Data Structures and Algorithms》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换