欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

比Math類庫abs()方法性能更高的取絕對值方法介紹

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

Math.abs()的實(shí)現(xiàn)源碼

通過三目運(yùn)算符判斷a是否小于0來實(shí)現(xiàn)

/**
  * Returns the absolute value of an {@code int} value.
  * If the argument is not negative, the argument is returned.
  * If the argument is negative, the negation of the argument is returned.
  *
  * <p>Note that if the argument is equal to the value of
  * {@link Integer#MIN_VALUE}, the most negative representable
  * {@code int} value, the result is that same value, which is
  * negative.
  *
  * @param a the argument whose absolute value is to be determined
  * @return the absolute value of the argument.
  */
 public static int abs(int a) {
  return (a < 0) ? -a : a;
 }

如果換種方式,性能會(huì)有20%左右的提升

代碼如下

 /**
  * Created by 譚健 2017/8/13. 12:47.
  * All Rights Reserved
  *
  * int 是 32 位數(shù)據(jù)
  * int 類型的任何正數(shù)右移31位 = 0,任何負(fù)數(shù)右移31位 = 1
  * 溢出 31 位截?cái)?,空?31 位補(bǔ)1,得到-1
  * a>>31 可以得到該數(shù)的符號(hào)位 + 還是 -
  * 如果 a>>31 + ,那么 a ^ 0 = a ,如果 a>>31 - ,那么 a ^ -1 翻轉(zhuǎn) a 的二進(jìn)制
  *
  * @param a int a
  * @return a 的絕對值
  */
 public static int abs(int a){
  return (a^(a>>31))-(a>>31);
 }

奇數(shù)偶數(shù)的判斷

/**
  * 一般普遍采用 n % 2 == 0 的方式
  * 但是如果換成位運(yùn)算方式,效率會(huì)比前者好很多
  *
  * 在二進(jìn)制中,末位為 0 必然是偶數(shù),否則是奇數(shù),并且不論正負(fù)
  * 所以,是什么數(shù),看看末位就行了
  *
  * @param a long a
  * @return 如果是奇數(shù),返回true,否則返回false
  */
 public static boolean isOdd(long a){
  return (a & 1) == 1;
 }

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對我們的支持。

上一篇:C#ComboBox控件“設(shè)置 DataSource 屬性后無法修改項(xiàng)集合”的解決方法

欄    目:C#教程

下一篇:C# 8.0可空引用類型的使用注意記錄

本文標(biāo)題:比Math類庫abs()方法性能更高的取絕對值方法介紹

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4787.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有