Preprint
Article

This version is not peer-reviewed.

Exact Identities for the Binary Hamming Weight Under Arithmetic and Bitwise Operations

Submitted:

31 October 2025

Posted:

03 November 2025

You are already at the latest version

Abstract
We collect and prove exact identities for the binary digital sum S2(n)—the Hamming weight wt (n)—under elementary arithmetic and bitwise operations. For x, y ≥ 0 we derive explicit carry/borrow decompositions of wt(x + y) and wt (xy) in terms of bitwise carries/borrows ci, bi (0-based indexing, c0 = b0 = 0). We restate classical XOR/OR/AND weight identities in a unified notation, give shift–mask lemmas yielding constructive corollaries (e.g., forcing a prescribed Hamming weight), and present a 2-adic reformulation linked to Kummer’s theorem. We also discuss algorithmic, hardware, and side-channel applications. Proofs are elementary and self-contained.
Keywords: 
;  ;  ;  ;  ;  

1. Notation and Preliminaries

We work with nonnegative integers in binary. Bits are indexed from 0 (LSB). For n 0 ,
wt ( n ) = i 0 n i , n = i 0 n i 2 i , n i { 0 , 1 } .
Let x , y 0 . In x + y , let c i = c i ( x , y ) { 0 , 1 } be the carry entering bit i (set c 0 = 0 ), and write c i + 1 for the carry exiting bit i. In x y with x y , define b i = b i ( x , y ) analogously with b 0 = 0 . For bitwise operations we use x XOR y , x AND y , x OR y , and shifts x k , x k .
Proposition 1 
(Folklore identities). For all x , y 0 ,
wt ( x XOR y ) = wt ( x ) + wt ( y ) 2 wt ( x AND y ) ,
wt ( x OR y ) = wt ( x ) + wt ( y ) wt ( x AND y ) .
Proof. 
Count ones positionwise: XOR marks disagreement; OR marks at-least-one. □

2. Carry and Borrow Decompositions

Fix m 0 such that x , y < 2 m + 1 . All sums below are taken over i = 0 , , m . We also make c m + 1 and b m + 1 explicit (final carry/borrow).
Theorem 1 
(Addition: exact carry decomposition). For all x , y 0 , with c 0 = 0 ,
wt ( x + y ) = wt ( x ) + wt ( y ) + i = 0 m c i 2 c i + 1 = wt ( x ) + wt ( y ) i = 0 m ( c i + 1 c i ) c m + 1 .
Proof. 
At bit i: x i + y i + c i = s i + 2 c i + 1 . Sum i = 0 . . m and identify x i = wt ( x ) , y i = wt ( y ) , s i = wt ( x + y ) . □
Theorem 2 
(Subtraction: exact borrow decomposition). Let x y 0 . With b 0 = 0 ,
wt ( x y ) = wt ( x ) wt ( y ) + i = 0 m b i + 1 b i + b m + 1 = wt ( x ) wt ( y ) i = 0 m ( b i 2 b i + 1 ) .
For x y we have b m + 1 = 0 .
Remark 1 
(Carries and 2-adic valuation). The number of carries in adding x and y equals the 2-adic valuation of x + y x :
# { i 0 : c i + 1 = 1 } = v 2 x + y x .

3. Shift–Mask Tools and Constructive Corollaries

Lemma 1 
(Shifts). For k 0 , wt ( x k ) = wt ( x ) and wt ( x k ) wt ( x ) .
Lemma 2 
(Mask disjointness). Let M = j = a a + 1 2 j be a run of ℓ consecutive 1s, disjoint from the support of x. Then wt ( x OR M ) = wt ( x ) + = wt ( x XOR M ) .
Corollary 1 
(Forcing a target Hamming weight). For any x and any integer t wt ( x ) , there exists a mask M (a disjoint run of ones) such that wt ( x OR M ) = t .

4. Applications and Bounds

Write L = bl ( max { x , y } ) = log 2 ( max { x , y } ) + 1 for the active bit-length, and C = i = 0 m c i + 1 for the total number of carries (so C = v 2 x + y x by rem:kummer).
Corollary 2 
(Bit-length lower bound). From (3) and c i 0 we have
wt ( x + y ) wt ( x ) + wt ( y ) 2 C wt ( x ) + wt ( y ) 2 L ,
since C L . This bound is often sharp up to an additive constant.
Corollary 3 
(Kummer-based control). Using C = v 2 x + y x ,
wt ( x + y ) = wt ( x ) + wt ( y ) + i = 0 m c i 2 v 2 x + y x .
Hence any upper bound on v 2 x + y x (e.g., via binary digit overlaps of x and y) yields a corresponding lower bound on wt ( x + y ) .
Corollary 4 
(Bitwise combinations). For any x , y ,
wt ( x XOR y ) wt ( x ) + wt ( y ) , wt ( x OR y ) wt ( x ) + wt ( y ) ,
with equality iff x AND y = 0 .

Algorithmic angle

Computing wt ( x + y ) directly is O ( L ) bit-operations (same as addition). The identity (3) expresses wt ( x + y ) as a linear form in the carry profile; thus any algorithm or hardware that already exposes carries (e.g., prefix adders like Kogge–Stone/Brent–Kung) gives wt ( x + y )  for free after addition (one pass of counting). This is useful in:
  • Complexity accounting in bit-level algorithms: tight bounds on post-addition Hamming weights in dynamic programming or bitset convolution.
  • Word-parallel trickery: when maintaining population counts under incremental updates, (3) predicts the decrement driven by carry chains.

Hardware angle

In CMOS, dynamic power correlates with bit flips and, in many platforms, with Hamming weights of buses. The carry chain length C (and its distribution) interacts with toggle activity; (3) isolates the linear influence of C on the resulting weight. This connects to prefix adder analyses (Kogge–Stone [6], Brent–Kung [7]).

Side-channel angle

Simple/Differential Power Analysis often models traces via Hamming weight or Hamming distance. Identity (3) provides a deterministic link between the weight after addition and the carry chain (which itself depends on operand bit patterns), informing leakage simulators and countermeasures (e.g., operand randomization). See Kocher–Jaffe–Jun [9].
Remark 2 
(Towards multiplicative operations). For multiplication, x y = i x i ( y i ) is a sum of wt ( x ) shifted copies of y; hence
wt ( x y ) wt ( x ) bl ( y )
by a naive union bound (collisions and carries can reduce the weight). A refined analysis requires carry bookkeeping across the partial-product convolution; we leave a tight identity as future work.

5. Worked Examples and Sanity Checks

Bits are indexed from 0. We verify Theorems 1 and 2 on small pairs.

Example A (Addition)

Let x = 29 = ( 11101 ) 2 , y = 23 = ( 10111 ) 2 . Then x + y = 52 = ( 110100 ) 2 , wt ( 52 ) = 3 . Active carries:
c 0 = 0 , c 1 = c 2 = c 3 = c 4 = c 5 = 1 , c 6 = 0 .
Thus i = 0 5 ( c i 2 c i + 1 ) = ( 0 2 ) + ( 1 2 ) + ( 1 2 ) + ( 1 2 ) + ( 1 2 ) + ( 1 0 ) = 5 . By (3):
wt ( x + y ) = wt ( x ) + wt ( y ) + i = 0 5 ( c i 2 c i + 1 ) = 4 + 4 5 = 3 .

Example B (Subtraction)

Let x = 53 = ( 110101 ) 2 , y = 19 = ( 10011 ) 2 . Then x y = 34 = ( 100010 ) 2 has wt = 2 . A borrow trace gives an active window with ( b i + 1 b i ) = 0 , hence wt ( x y ) = wt ( x ) wt ( y ) = 4 3 = 1 plus a single bit created by the borrow cascade, totaling 2, matching (4).
A small table:
Table 1. Sanity checks consistent with (3) and (4).
Table 1. Sanity checks consistent with (3) and (4).
x y wt ( x ) wt ( y ) x + y wt ( x + y ) x y ( 0 ) wt ( x y )
5 3 2 2 8 1 2 1
29 23 4 4 52 3 6 2
37 14 3 3 51 3 23 4

6. 2-Adic Perspective and Kummer Linkage

Kummer’s theorem states that the exponent of a prime p dividing u v equals the number of carries when adding v and u v in base p. For p = 2 ,
v 2 x + y x = # ( carries in adding x and y ) .
Combining with thm:add yields an explicit reformulation of wt ( x + y ) in terms of v 2 x + y x and { c i } .

7. Positioning, Prior Art, and Contributions

Digital sums in base 2 go back to Delange and Coquet; Allouche–Shallit systematized many properties via automata. The identities here are elementary but presented as a consolidated, explicit package with constructive corollaries and a 2-adic bridge. Connections to prefix adders and leakage models highlight practical relevance.

8. Conclusions

We provided exact weight identities under addition, subtraction, and bitwise operations; shift–mask tools; bounds in terms of bl and Kummer; and application pointers (algorithms, hardware, side-channels). Future work: tight multiplicative identities and carry-profile statistics.

References

  1. H. Delange, Sur la fonction sommatoire de la fonction somme des chiffres, Enseign. Math. 1975, 21, 31–47.
  2. J. Coquet, A summation formula related to the binary digits. Invent. Math. 1983, 73, 107–115. [CrossRef]
  3. J.-P. Allouche and J. Shallit, Automatic Sequences: Theory, Applications, Generalizations, Cambridge Univ. Press, 2003.
  4. E. E. Kummer, Über die ergänzungssätze zu den allgemeinen Reciprocitätsgesetzen. J. Reine Angew. Math. 1852, 44, 93–146.
  5. P. Flajolet and R. Sedgewick, Analytic Combinatorics, Cambridge Univ. Press, 2009.
  6. P. M. Kogge and H. S. Stone, A parallel algorithm for the efficient solution of a general class of recurrences. IEEE Trans. Comput. 1973, C-22, 786–793. [CrossRef]
  7. R. P. Brent and H. T. Kung, A regular layout for parallel adders. IEEE Trans. Comput. 1982, C-31, 260–264. [CrossRef]
  8. D. E. Knuth, The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, 3rd ed., Addison-Wesley, 1998.
  9. P. Kocher, J. Jaffe, and B. Jun, Differential Power Analysis, CRYPTO 1999, LNCS 1666, 388–397.
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content.
Copyright: This open access article is published under a Creative Commons CC BY 4.0 license, which permit the free download, distribution, and reuse, provided that the author and preprint are cited in any reuse.
Prerpints.org logo

Preprints.org is a free preprint server supported by MDPI in Basel, Switzerland.

Subscribe

Disclaimer

Terms of Use

Privacy Policy

Privacy Settings

© 2026 MDPI (Basel, Switzerland) unless otherwise stated