Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя BinaryBelle

Автор BinaryBelle, история, 3 года назад, По-английски

Do you know the exact difference between bitmasking and bit manipulation?

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
»
3 года назад, # |
  Проголосовать: нравится +2 Проголосовать: не нравится

Bit Manipulation is a general term that refers to performing calculations using bitwise operations(For example computing powers of 2, getting set and unset bits, etc., can be done by Bit Manipulation).

Whereas, Bitmasking is a technique where we use an unsigned integer to represent an entity and perform various operations "on that entity" efficiently using Bit Manipulation(Since we are dealing with unsigned integers).

For example, let's consider an array of strings arr = {"abc", "def", "ccc", "ade"}. Here, we can represent each string in the array using an unsigned integer(which is called a bitmask. Mask means to hide something and bitmask means using the binary representation of unsigned integers to hide something). We consider each string as a 26-bit binary number where a bit is set if the corresponding character at that index('a' represents the 0th bit, 'b' represents the 1st bit, and so on...till 'z' representing the 25th bit) is present in the string else the bit is unset. So, using this knowledge we can convert the above array into

=> arr = {"...000111", "...0111000", "...00100", "...0011001"}

=> arr = {7, 56, 4, 25}.

On this converted array, we can efficiently perform operations such as

  1. Insertion of characters
  2. Deletion of characters
  3. Checking if a character is present in the string
  4. Comparing two strings etc.,

using Bit Manipulation.

Now, this is just one of the many use cases of "Bit Masking". And we say "Bit Manipulation" whenever we use a bitwise operation.