algorithm - Can I assume that a bitwise AND operation is O(1)? If so why? -


my processor can arithmetic operations on 8-bit or 16-bit unsigned integers.

1) means word size processor 16 bits, correct?

operations on words o(1).

the reason has actual circuit-level implementation of how processor works, correct?

if add 2 words, , result number more 16-bits, state following?

1) processor can add numbers report 16 least significant digits.

2) report more 16 bits, processor must have software permits these operations large numbers (numbers don't fit in word).

finally,

let's have word w 16-bit digit , want 8 least significant digits. can w & 0xff. time complexity of operation? o(1) because of circuit level implementation of processor well?

short answer:

yes, single bitwise , considered o(1).

a little more detail:

even if looked @ number of operations on each bit still o(1). actual number of bit operations may vary variable type, e.g. 8 bits vs. 16 bits vs. 32 bits vs. 64 bits (even 128 bits or more). key no matter underlying machine using, still constant number of operations perform it. computers develop on time, bitwise , still o(1).

another example add clarification

the following block of code o(1):

print('hello world'); print('hello world'); print('hello world'); 

although print hello world 3 times, every time run it, take constant amount of time run , operate , doesn't take longer if feeds large data set program. it'll print 3 things, no matter input.

in case of bitwise and, perform specified number of sub-operations same number. e.g. 8, 16, 32, etc. 1 operation, same or constant.

in example, sounds trying show have operations not require of bits perform well. if these smaller operations considered 4 bits of 8. code constant number of operations when hits code. printing 4 hello world statements instead of 8 hello world statements. either way, 4 or 8 prints, still constant.

this why single bitwise , operation o(1).


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -