goden1607

package
v0.0.0-...-b071cee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2023 License: GPL-3.0 Imports: 1 Imported by: 0

README

面试题 16.07.最大数值

1. 题目描述

编写一个方法,找出两个数字 ab 中最大的那一个。不得使用if-else或其他比较运算符。 示例:

输入: a = 1, b = 2
输出: 2

标签 位运算 脑筋急转弯 数学

2. 解题

对于算式a * k + b * (k ^ 1),若当a>b时k=1, 可返回a,当a<b时k=0,可返回b

那么如何获得k?

负数的二进制表示最高位为1,若取k = (b-a)>>63 & 0x1, 则可获取k

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL