當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang bits.ReverseBytes()用法及代碼示例

Go語言提供了對位的內置支持,以借助bits包對預聲明的無符號整數類型實現位計數和操作函數。該程序包提供ReverseBytes()函數,該函數用於查找a值的倒序。要訪問ReverseBytes()函數,您需要借助import關鍵字在程序中添加數學/位程序包。

用法:

func ReverseBytes(a uint) uint

參數:此函數采用uint類型的一個參數,即a。

返回值:此函數以相反的順序返回其位的a值。

範例1:



// Golang program to illustrate  
// bits.ReverseBytes() Function 
  
package main 
  
import ( 
    "fmt"
    "math/bits"
) 
  
// Main function 
func main() { 
  
    // Finding the reverse order of a 
    // Using ReverseBytes() function 
    a:= bits.ReverseBytes(7) 
    fmt.Printf("Reverse order of %d:%b", 7, a) 
  
}

輸出:

Reverse order of 7:11100000000000000000000000000000000000000000000000000000000

範例2:

// Golang program to illustrate 
// bits.ReverseBytes() Function 
package main 
  
import ( 
    "fmt"
    "math/bits"
) 
  
// Main function 
func main() { 
  
    // Finding the reverse order of a 
    // Using ReverseBytes() function 
    a1:= bits.ReverseBytes(3) 
    fmt.Printf("ReverseBytes(%b):= %b\n", 3, a1) 
  
    a2:= bits.ReverseBytes(7) 
    fmt.Printf("ReverseBytes(%b):= %b\n", 7, a2) 
  
}

輸出:

ReverseBytes(11):= 1100000000000000000000000000000000000000000000000000000000
ReverseBytes(111):= 11100000000000000000000000000000000000000000000000000000000



相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 bits.ReverseBytes() Function in Golang with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。