当前位置:网站首页>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
边栏推荐
- 指针进阶,字符串函数
- GoLand set goproxy
- LeetCode 715. Range 模块
- Tips for using jeditabletable
- POJ - 3784 running medium
- Data analysis methodology and previous experience summary 2 [notes dry goods]
- How to integrate app linking services in harmonyos applications
- 为什么要选择云原生数据库
- 快速集成认证服务-HarmonyOS平台
- MAC OSX php dyld: Library not loaded: /usr/local/xxxx. dylib
猜你喜欢
【踩坑】nacos注册一直连接localhost:8848,no available server
let const
Markdown editor Use of MD plug-in
Quick sorting (detailed illustration of single way, double way, three way)
[南京大学]-[软件分析]课程学习笔记(一)-introduction
PPT模板、素材下载网站(纯干货,建议收藏)
Image segmentation in opencv
Greenplum6.x-版本变化记录-常用手册
Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
[step on the pit] Nacos registration has been connected to localhost:8848, no available server
随机推荐
Quick sorting (detailed illustration of single way, double way, three way)
登山小分队(dfs)
Skills that testers must know: Selenium's three waiting ways are interpreted clearly
Compilation and linking of programs
平台化,强链补链的一个支点
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - package based on thinkphp5 - detailed guidance of the author - July 6, 2022 - Novice
Frequently Asked Coding Problems
Greenplum6.x搭建_环境配置
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
Gson转换实体类为json时报declares multiple JSON fields named
Leetcode 1984. Minimum difference in student scores
测试人一定要会的技能:selenium的三种等待方式解读,清晰明了
Required String parameter ‘XXX‘ is not present
Go write a program that runs within a certain period of time
Mountaineering team (DFS)
Greenplum6.x常用语句
NCS Chengdu Xindian interview experience
如何在HarmonyOS应用中集成App Linking服务
注解@ConfigurationProperties的三种使用场景
Speaking of a software entrepreneurship project, is there anyone willing to invest?