当前位置:网站首页>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
边栏推荐
- Leetcode 1984. Minimum difference in student scores
- String operation
- Why choose cloud native database
- [Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
- opencv 将16位图像数据转为8位、8转16
- [Yugong series] February 2022 U3D full stack class 006 unity toolbar
- Recommended by Alibaba P8, the test coverage tool - Jacobo is very practical
- 实现自定义内存分配器
- 数据库存储---表分区
- How to integrate app linking services in harmonyos applications
猜你喜欢
Rapid integration of authentication services - harmonyos platform
leetcode134. gas station
南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
xray的简单使用
How to integrate app linking services in harmonyos applications
登山小分队(dfs)
Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
Componentspace2022, assertions, protocols, bindings, and configuration files
LeetCode 715. Range module
随机推荐
MySQL partition explanation and operation statement
Enterprise manager cannot connect to the database instance
Selenium automation integration, eight years of testing experience, soft test engineer, an article to teach you
Other 7 features of TCP [sliding window mechanism ▲]
测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?
[Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
Find the original code, inverse code and complement of signed numbers [C language]
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
更改当前文件夹及文件夹下文件日期shell脚本
[Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
说一个软件创业项目,有谁愿意投资的吗?
年薪50w阿裏P8親自下場,教你如何從測試進階
MAC OSX php dyld: Library not loaded: /usr/local/xxxx. dylib
Greenplum6.x重新初始化
关于基于kangle和EP面板使用CDN
ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
Mock.js用法详解
Pointer advanced, string function
如何在HarmonyOS应用中集成App Linking服务