algorithm - Are bit-wise operations common and useful in real-life programming? -


i bump interview questions involving sorted/unsorted array , ask find sort of property of array. example finding number appears odd number of times in array, or find missing number in unsorted array of size 1 million. question post additional constraints such o(n) runtime complexity, or o(1) space complexity. both of these problems can solved pretty efficiently using bit-wise manipulations. of course these not all, there's whole ton of questions these.

to me bit-wise programming seems more hack or intuition based, because works in binary not decimals. being college student not real life programming experience @ all, i'm curious if questions of type popular @ in real work, or brain twisters interviewers use select smartest candidate. if indeed useful, in kind of scenarios applicable?

are bit-wise operations common , useful in real-life programming?

the commonality or applicability depends on problem in hand.

some real-life projects benefit bit-wise operations.

some examples:

  1. you're setting individual pixels on screen directly manipulating video memory, in every pixel's color represented 1 or 4 bits. so, in every byte can have packed 8 or 2 pixels , need separate them. basically, hardware dictates use of bit-wise operations.

  2. you're dealing kind of file format (e.g. gif) or network protocol uses individual bits or groups of bits represent pieces of information. data dictates use of bit-wise operations.

  3. you need compute kind of checksum (possibly, parity or crc) or hash value , of applicable algorithms manipulating bits.

  4. you're implementing (or using) arbitrary-precision arithmetic library.

  5. you're implementing fft , naturally need reverse bits in integer or simulate propagation of carry in opposite direction when adding. nature of algorithm requires bit-wise operations.

  6. you're short of space , need use little memory possible , squeeze multiple bit values , groups of bits entire bytes, words, double words , quad words. choose use bit-wise operations save space.

  7. branches/jumps on cpu costly , want improve performance implementing code series of instructions without branches , bit-wise instructions can help. simplest example here choosing minimum (or maximum) integer value out of two. natural way of implementing kind of if statement, involves comparison , branching. choose use bit-wise operations improve speed.

  8. your cpu supports floating point arithmetic calculating square root slow operation , instead simulate using few fast , simple integer , floating operations. same here, benefit manipulating bit representation of floating point format.

  9. you're emulating cpu or entire computer , need manipulate individual bits (or groups of bits) when decoding instructions, when accessing parts of cpu or hardware registers, when emulating bit-wise instructions or, and, xor, not, etc. problem flat out requires bit-wise instructions.

  10. you're explaining bit-wise algorithms or tricks or needs bit-wise operations else on web (e.g. here) or in book. :)

i've done of above , more in past 20 years. ymmv, though.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -