当前位置:网站首页>Average two numbers
Average two numbers
2022-07-04 06:02:00 【AppNinja】
// average.cpp : Defines the entry point for the console application .
//
#include "stdafx.h"
// There is Bug edition : Integer overflow
unsigned average1(unsigned a, unsigned b)
{
return (a + b) / 2;
}
// The following is the correct version :
unsigned average2(unsigned low, unsigned high)
{
return low + (high - low) / 2;
}
unsigned average3(unsigned a, unsigned b)
{
return (a / 2) + (b / 2) + (a & b & 1);
}
unsigned average4(unsigned a, unsigned b)
{
return (a & b) + (a ^ b) / 2;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a = 10;
int b = 7;
unsigned int result = average4(0x80000000U, 0x80000000U);
printf("%d", result);
return 0;
}
On finding the average of two unsigned integers without overflow - The Old New Thing (microsoft.com)
边栏推荐
- Sword finger offer II 038 Daily temperature
- 复合非线性反馈控制(二)
- Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
- Experience weekly report no. 102 (July 4, 2022)
- How to solve the component conflicts caused by scrollbars in GridView
- Invalid revision: 3.18.1-g262b901-dirty
- Kubernets first meeting
- Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
- C语言中的函数(详解)
- AWT常用组件、FileDialog文件选择框
猜你喜欢
ES6 modularization
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
Canoe panel learning video
QT QTableWidget 表格列置顶需求的思路和代码
【微服务】Nacos集群搭建以及加载文件配置
Sword finger offer II 038 Daily temperature
Configure cross compilation tool chain and environment variables
【无标题】
如何实现视频平台会员多账号登录
卸载Google Drive 硬盘-必须退出程序才能卸载
随机推荐
我的NVIDIA开发者之旅——优化显卡性能
left_and_right_net正常版本
Actual cases and optimization solutions of cloud native architecture
"In simple language programming competition (basic)" part 1 Introduction to language Chapter 3 branch structure programming
JSON web token -- comparison between JWT and traditional session login authentication
Practical gadget instructions
(4) Canal multi instance use
How to implement lazy loading in El select (with search function)
Weekly summary (*63): about positive energy
Sword finger offer II 038 Daily temperature
How much computing power does transformer have
JS arguments parameter usage and explanation
lightroom 导入图片灰色/黑色矩形 多显示器
One click filtering to select Baidu online disk files
LC weekly 300
Halcon image calibration enables subsequent image processing to become the same as the template image
BUU-Pwn-test_ your_ nc
Leakage detection relay jy82-2p
JSON Web Token----JWT和傳統session登錄認證對比
The end of the Internet is rural revitalization