Beginner

位元計數與位元長度計算機

計算整數含有多少個 1 位元、測量其位元長度,並即時判斷是否為 2 的冪次。
Non-negative whole number

Input base

bits

Total bits for zero count (defaults to bit length)
Set bits (popcount)
4

Hamming weight of the binary representation

8

bits

Set bits (1s)

50%

Zero bits (0s)

50%

Zero bits
4
Bit length
8
Width used
8
Power of two
Binary
10011100
Hexadecimal
0x9C
Step by step
  1. 1

    Binary representation

    10011100
    Bit length: 8
  2. 2

    Set bits (1s counted)

    count of 1s in 10011100 = 4
計算結果僅為一般資訊用途的估算值,並非專業建議——在仰賴重要結果之前,請務必自行獨立查證。 閱讀完整免責聲明.
快速解答

此計算機如何運作?

Popcount 是整數中 1 位元的計數(漢明重量),透過逐位移位和遮罩來計算。位元長度是最高置位位元的位置。一個數恰好是 2 的冪次,當且僅當其 popcount 等於 1。以十進位或二進位輸入 n,即可即時看到全部四個值。

公式
popcount = n 中 1 位元的個數; 位元長度 = 最高置位位元的位置; 2 的冪次 ⇔ popcount = 1
How this is calculated

以十進位或二進位格式輸入非負整數 n。該值以任意精度整數解析,因此非常大的數字也不會溢位。Popcount(又稱漢明重量)是 1 位元的計數:反覆用 n & 1 檢查最低位元,再用 n >> 1 右移,直到 n 歸零,同時累計置位位元數。

位元長度是表示 n 所需的位元數,等於最高置位位元的位置(n = 0 的位元長度為 0)。可選的寬度欄位設定固定欄位大小 w;零位元數則為 w − popcount。若不填寬度,預設為位元長度,因此零計數僅反映數字本身內部的位元。

一個數恰好是 2 的冪次,當且僅當它只有一個置位位元,即 popcount = 1(因此 1、2、4、8、16、…符合,但 0 不符合)。所有計數均為精確整數;若所填的寬度小於位元長度,將自動提升至位元長度,使零計數不為負數。

常見問題

Popcount 是一個數的二進位表示中設為 1 的位元數,又稱漢明重量,廣泛應用於錯誤更正、雜湊和低階最佳化。

零沒有置位位元,因此其位元長度為 0,popcount 亦為 0。每個正整數的位元長度等於其最高置位位元索引加一。

2 的冪次只有一個 1 位元,後跟全是零(二進位為 1、10、100、1000)。由於只有一個位元被設定,其 popcount 恰好為 1,這是檢驗此屬性最快的方法。

也稱為

位元計數
popcount
設定位元數
漢明重量
位元長度
計算二進位1的個數
set bit count
bit length

APA

TG we-Calculate Editorial Team. (2026). 位元計數與位元長度計算機 [Online calculator]. TG we-Calculate. https://we-calculate.com/zh-tw/calculator/popcount-bit-length-calculator

Chicago

TG we-Calculate Editorial Team. "位元計數與位元長度計算機." TG we-Calculate. 2026. https://we-calculate.com/zh-tw/calculator/popcount-bit-length-calculator.

IEEE

TG we-Calculate Editorial Team, "位元計數與位元長度計算機," TG we-Calculate, 2026. [Online]. Available: https://we-calculate.com/zh-tw/calculator/popcount-bit-length-calculator

BibTeX

@misc{wecalculate_popcount_bit_length_calculator, title = {位元計數與位元長度計算機}, author = {{TG we-Calculate Editorial Team}}, howpublished = {\url{https://we-calculate.com/zh-tw/calculator/popcount-bit-length-calculator}}, year = {2026}, note = {TG we-Calculate} }

這個計算機對您有幫助嗎?