当前位置:网站首页>Several methods of calculating the average value of two numbers
Several methods of calculating the average value of two numbers
2022-07-07 08:52:00 【Please Sit Down】
Method 1 : Directly find the average ( Add two numbers and divide by two )
int average(int x,int y){
return (x + y) / 2;
}This algorithm has some defects , When x and y When large enough x and y And there will be data overflow , So we can't get the result we want .( It is not recommended to use )
Method 2 :x + y And x Divide by 2 And , Data overflow can be avoided
int average(int x,int y){
return x + (y - x) / 2;
}Method 3 : Improvement method version II ( Displacement )
int average(int x,int y){
return x + ((y - x) >> 1);
}Method four : Using bit operations - Intersection plus half of difference ( Rounding down )
int average(int x,int y){
return (x & y) + ((x ^ y) >> 1);
}Method five : Using bit operations - Intersection plus half of difference ( Rounding up )
int average(int x,int y){
return (x | y) - ((x ^ y) >> 1);
}Method four 、 V see link : The magic of bit operation - Find the average of two integers - You know
边栏推荐
- 关于基于kangle和EP面板使用CDN
- 测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?
- String operation
- Qt Charts使用(重写QChartView,实现一些自定义功能)
- Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
- Routing information protocol rip
- 模拟卷Leetcode【普通】1705. 吃苹果的最大数目
- Why choose cloud native database
- Un salaire annuel de 50 W Ali P8 vous montrera comment passer du test
- Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
猜你喜欢

Compilation and linking of programs

如何在HarmonyOS应用中集成App Linking服务

详解华为应用市场2022年逐步减少32位包体上架应用和策略

Greenplum6.x-版本变化记录-常用手册

Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up

Greenplum 6.x version change record common manual

Markdown编辑器Editor.md插件的使用

Quick sorting (detailed illustration of single way, double way, three way)

NCS Chengdu Xindian interview experience

阿里p8推荐,测试覆盖率工具—Jacoco,实用性极佳
随机推荐
如何在快应用中实现滑动操作组件
[wechat applet: cache operation]
[Yugong series] February 2022 U3D full stack class 005 unity engine view
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
JS operation
Qt Charts使用(重写QChartView,实现一些自定义功能)
Leetcode 1984. Minimum difference in student scores
指针进阶,字符串函数
【踩坑】nacos注册一直连接localhost:8848,no available server
对API接口或H5接口做签名认证
GoLand set goproxy
[step on the pit] Nacos registration has been connected to localhost:8848, no available server
[Yugong series] February 2022 U3D full stack class 006 unity toolbar
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
You should use Google related products with caution
[Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
Rapid integration of authentication services - harmonyos platform
Mock. JS usage details
Selenium automation integration, eight years of testing experience, soft test engineer, an article to teach you
Gson转换实体类为json时报declares multiple JSON fields named