当前位置:网站首页>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 |
边栏推荐
- 离线数仓-用户行为采集
- Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
- C语言 结构体定义方法
- 磷脂-聚乙二醇-巯基,DSPE-PEG-Thiol,DSPE-PEG-SH,MW:5000
- COCO数据集训练TPH-YoloV5
- UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu
- 赶紧进来!!!教你用C语言写三子棋小游戏
- parser = argparse.ArgumentParser() parsing
- [Learning Records of Boxue Valley] Super summary, share with heart | Software Testing Interface Testing Basics
- PCL—point cloud data segmentation
猜你喜欢
离线数仓-用户行为采集
querystring模块
The @autowired distinguished from @ the Resource
@DateTimeFormat注解
[Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)
PCL—point cloud data segmentation
MySQL常见的索引
UserWarning:火炬。meshgrid:在以后的版本中,它将被要求通过索引ing argu
HCIP-第十一天-MPLS+BGP
「PHP基础知识」空值(null)的使用
随机推荐
磷脂-聚乙二醇-叠氮,DSPE-PEG-Azide,DSPE-PEG-N3,MW:5000
3分钟带你了解微信小程序开发
排序学习笔记(二)堆排序
@ApiModel 和 @ApiModelProperty
Problems when yolov5 calls ip camera
kettle 安装与配置
删库后!除了跑路还能干什么?
String comparison size in MySQL (date string comparison problem)
MySQL8.0与MySQL5.7差异分析
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
getattr() function analysis
【面经】米哈游数据开发一面二面面经
cross-domain problem solving
debian 10 nat 与路由转发
C语言的变长数组
解决glob()返回文件排序不一致问题&onnx本地按照安装方法
JJWT tool class
basic operator
MySQL分组后排序
我的两周年创作纪念日