当前位置:网站首页>C语言中关于2的n次方求值问题(移位运算)
C语言中关于2的n次方求值问题(移位运算)
2022-08-02 03:22:00 【凉世弥音九月秋】
最近,看到一个有趣的题目:不使用累计乘法的基础上,通过移位运算(<<)实现2的n次方的计算
起初,我看到这道题是一脸懵逼,后来查阅资料后发现就是一道简单的移位运算题。
好啦话不多说,放图:
#include<stdio.h>
int main(void)
{
int num;
int p;
while (scanf("%d", &num)) {
p = 1 << num;
printf("%d\n",p);
}
return 0;
}
其中移位运算包括左移和右移
左移运算:x<<k:x左移k位,并在右端补零
右移运算(逻辑):x>>k:x右移k位,并在左端补零
右移运算(算术):a.x>>k:x右移k位,并在左端补零(注意:此时符号位应为0,即正)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ b.x>>k:x右移k位,并在左端补1(注意:此时符号应为1,即负)
参数x | 10010101 |
---|---|
x<<2 | 01010100 |
x>>2(逻辑) | 00100101 |
x>>2(算术) | 11100101 |
边栏推荐
- Basic usage of Monaco Editor
- mysql卸载详细教程
- APK的安装过程分析 PMS包管理器
- @Configuration详解
- 每天填坑,精卫填坑第二集,TX1 配置从固态启动,安装Pytorch
- IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
- String comparison size in MySQL (date string comparison problem)
- 排序学习笔记(二)堆排序
- [详解C语言]一文带你玩转C语言小游戏---三子棋
- [Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)
猜你喜欢
Week 7 Review
Redis的集群模式
「PHP基础知识」空值(null)的使用
nucleo stm32 h743 FREERTOS CUBE MX配置小记录
parser = argparse.ArgumentParser()解析
知识工程作业2:知识工程相关领域介绍
Redis安装,基本命令,持久化方式,集群
UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu
C语言 0长度数组/柔性数组
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
随机推荐
MySQL常见的索引
Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
Monaco Editor 的基本用法
啃瓜记录第一天
网站与服务器维护怎么做?
SSM integration
UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu
Mysql8创建用户以及赋权操作
[Learning Records of Boxue Valley] Super summary, share with heart | Software Testing Interface Testing Basics
[Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)
【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?
MySQL分页查询的5种方法
@Autowired注解的使用
C语言的变长数组
COCO数据集训练TPH-YoloV5
mysql卸载详细教程
PCL—点云数据分割
HCIP-第十天-BGP综合实验
redis进行持久化时,有新的写操作将如何解决——写时复制
@ApiModel 和 @ApiModelProperty