当前位置:网站首页>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)
边栏推荐
- Steady! Huawei micro certification Huawei cloud computing service practice is stable!
- 19. Framebuffer application programming
- [microservice] Nacos cluster building and loading file configuration
- js arguments参数使用和详解
- Weekly summary (*63): about positive energy
- Win10 clear quick access - leave no trace
- BUU-Real-[PHP]XXE
- 2022.7.3-----leetcode.556
- 安装 Pytorch geometric
- transformer坑了多少算力
猜你喜欢
剑指 Offer II 038. 每日温度
BUU-Pwn-test_ your_ nc
【无标题】
如何展开Collapse 的所有折叠面板
Uninstall Google drive hard drive - you must exit the program to uninstall
How to configure static IP for Kali virtual machine
【微服务】Nacos集群搭建以及加载文件配置
Notes and notes
Gridview出现滚动条,组件冲突,如何解决
High performance parallel programming and optimization | lesson 02 homework at home
随机推荐
js获取对象中嵌套的属性值
Google Chrome browser will support the function of selecting text translation
buuctf-pwn write-ups (8)
High performance parallel programming and optimization | lesson 02 homework at home
C language exercises (recursion)
实用的小工具指令
Learning multi-level structural information for small organ segmentation
Risc-v-qemu-virt in FreeRTOS_ Lock mechanism analysis of GCC
Gridview出现滚动条,组件冲突,如何解决
(4) Canal multi instance use
一键过滤选择百度网盘文件
QT qtablewidget table column top requirements ideas and codes
1.1 history of Statistics
How to determine whether an array contains an element
JS flattened array of number shape structure
Design and implementation of redis 7.0 multi part AOF
Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
How to clone objects
如何判断数组中是否含有某个元素
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout