So, I am reading the code from this site: http://www.geeksforgeeks.org/write-ac-program-to-find-the-parity-of-an-unsigned-integer/
And this shows how to determine if a number has even or odd parity. However, I do not understand why the execution efficiency is log (n). Here is the code for reference:
# include <stdio.h> # define bool int /* Function to get parity of number n. It returns 1 if n has odd parity, and returns 0 if n has even parity */ bool getParity(unsigned int n) { bool parity = 0; while (n) { parity = !parity; n = n & (n - 1); } return parity; }
Execution efficiency is O (log (n)), where nis the value of the integer that you pass. However, this is an unconventional way of using O notation.
n
, O ( , ), k = O (log2 (n)) O (k).
( , - Θ (s), s - , n, , - O (1)).
, 1, , , (1) (, p) n.
, Θ (p).
, n, log2 (n), , id O (log2 (n)).