当前位置:网站首页>Complete size comparison with bit operation
Complete size comparison with bit operation
2022-07-19 16:34:00 【Java baa】
Only bit operation is used to complete size comparison
How to judge without any comparison statement , You can return the larger of the two numbers
/** * @author Yang Siyuan * @version 1.0 * @title Bit operation completes size comparison * @date 2022/7/16 0:52 * @description original Not necessarily the optimal solution a < b => -1 a == b => -1 a > b => 1 */
public class BitComparisonOfSize {
// Use only !=
public static int compare(int a, int b) {
if (a >>> 31 != b >>> 31) return b >>> 31 - a >>> 31;
for (int i = 30; i > 0; i--) {
if (a >>> i != b >>> i) return a >>> i - b >>> i;
}
return 0;
}
// Do not use any comparison statement ( > < = != >= <= )
public static int compare2(int a, int b) {
return a - b >>> 31;
}
public static void main(String[] args) {
System.out.println(" Beginning of the test ");
for (int i = 0; i < 1000000; i++) {
int a = (int) (Math.random() * Integer.MIN_VALUE) * Math.random() < 0.5 ? 1 : -1;
int b = (int) (Math.random() * Integer.MIN_VALUE) * Math.random() < 0.5 ? 1 : -1;
int res = compare(a, b);
int res2 = compare2(a, b);
int acceptedAnswer = Integer.compare(a, b);
if (res != acceptedAnswer || res2 != acceptedAnswer) {
System.out.println(" error :a:" + a + ",b:" + b);
}
}
System.out.println(" End of test ");
}
}
Written in the back
Algorithm column : Algorithm column
Application of more bit operations :05. Simple application of bitmap and comparator
Welcome to your attention , Will often record some problems encountered in algorithm learning .
Please feel free to leave a message to discuss , Let me share with you , Know all but answer !
边栏推荐
- JMeter 21 天打开 day11
- The read image of OpenCV notes is de distorted and saved as a new image
- MySQL - adjust column constraints
- 总结阅读器与直播的奇怪交互:复习双迪技术
- MySQL - autoincrement constraint of table fields
- Expressions régulières MySQL
- Ch549/ch548 study notes 4 - timer
- How do new shareholders open accounts online? Is it safe to open accounts online?
- NFT市场格局仍未变化,Okaleido能否掀起新一轮波澜?
- JMeter 21 day clock in day09
猜你喜欢
Summary of common problems of SolidWorks assembly (updated at any time)
JMeter 21 day clock in day08
使用深度學習制作機器人大腦圖紙
Chapter 4: emerging, class instantiation strategy with constructor based on cglib
JMeter 21 天打卡 day13
Ch549/ch548 learning notes 2 - system clock
[foundation of deep learning] how to calculate convolution
Weekly resume of personal IP lab · issue 19
JMeter 21 天打卡 day10
Clouds want clothes, flowers want looks, spring breeze blows the sill, Revlon (romantic code implementation)
随机推荐
啥是图神经网络?
Ch549/ch548 learning notes 2 - system clock
野指针问题: 复习橙其技术
Using the quickexec Fiddler command line
Cause analysis of the red light of the server and soft switch status on the Vos client
MySQL 正则表达式
Ch549 / ch548 notes d'apprentissage 9 - procédure de traitement des paramètres du périphérique USB
Flask模板
软件测试培训不靠谱?花费3W学软件测试半年没找到工作?
Is it safe to open a fund account online? College students seek guidance
MySQL - unique key constraint for table fields
张朝阳夜跑33公里:直播聊物理 揭示“超级月亮”成因
JMeter 21 day clock in day09
Object memory layout and synchronized lock upgrade
二手房房价分析与预测(图表绘制避免中文乱码、饼形图、折线图、条形图(柱形图)、enumerate函数、数据清洗(lambda\map)、二手房预测(scikit-learn))
Does flush software charge for online account opening for stock speculation? Is it safe to open an account?
Using deep learning to make robot brain drawings
How do new shareholders open accounts online? Is it safe to open accounts online?
Clouds want clothes, flowers want looks, spring breeze blows the sill, Revlon (romantic code implementation)
MySQL - autoincrement constraint of table fields