Bitmask

From WikiMD's Wellness Encyclopedia

Bitmask A bitmask is a sequence of bits that can be used to manipulate and query the individual bits of a binary number. Bitmasks are commonly used in computer science and programming for tasks such as setting, clearing, and toggling specific bits within a byte or word. They are also used in networking, cryptography, and various other fields where binary data manipulation is required.

Overview[edit | edit source]

A bitmask is typically represented as a binary number, where each bit in the mask corresponds to a bit in the target data. By using bitwise operations such as AND, OR, XOR, and NOT, specific bits in the target data can be manipulated according to the bitmask.

Common Bitwise Operations[edit | edit source]

  • AND ( & ): Used to clear specific bits. Only the bits that are set in both the bitmask and the target data will remain set.
  • OR ( | ): Used to set specific bits. Any bit that is set in the bitmask will be set in the target data.
  • XOR ( ^ ): Used to toggle specific bits. Any bit that is set in the bitmask will be inverted in the target data.
  • NOT ( ~ ): Used to invert all bits in the target data.

Applications[edit | edit source]

Bitmasks are used in various applications, including:

  • File permissions: In operating systems like Unix and Linux, file permissions are often represented using bitmasks.
  • Networking: Bitmasks are used in subnetting to define network and host portions of an IP address.
  • Graphics programming: Bitmasks are used to manipulate pixel data and perform operations like masking and blending.
  • Cryptography: Bitmasks are used in various cryptographic algorithms to manipulate binary data.

Example[edit | edit source]

Consider a byte (8 bits) of data: `11001100`. To clear the third bit from the right, we can use the bitmask `11111011` and the AND operation: ```

 11001100

& 11111011

 --------
 11001000

``` The result is `11001000`, where the third bit from the right has been cleared.

Related Pages[edit | edit source]

Template:Compu-science-stub

WikiMD
Navigation: Wellness - Encyclopedia - Health topics - Disease Index‏‎ - Drugs - World Directory - Gray's Anatomy - Keto diet - Recipes

Search WikiMD

Ad.Tired of being Overweight? Try W8MD's physician weight loss program.
Semaglutide (Ozempic / Wegovy and Tirzepatide (Mounjaro / Zepbound) available.
Advertise on WikiMD

WikiMD's Wellness Encyclopedia

Let Food Be Thy Medicine
Medicine Thy Food - Hippocrates

WikiMD is not a substitute for professional medical advice. See full disclaimer.
Credits:Most images are courtesy of Wikimedia commons, and templates Wikipedia, licensed under CC BY SA or similar.

Contributors: Prab R. Tumpati, MD