Skip to content

Instantly share code, notes, and snippets.

@OkiStuff
Created October 12, 2022 19:56
Show Gist options
  • Save OkiStuff/0166b958cdc672f6d8623151d9ac5b20 to your computer and use it in GitHub Desktop.
Save OkiStuff/0166b958cdc672f6d8623151d9ac5b20 to your computer and use it in GitHub Desktop.
Resource to learn more about Bitwise Operators

Title: Bitwise Operators Description: Bitwise Operators in Computer Science Date Created: 2022-10-12 License: GNU General Public License v3.0 Author: Frankie A. / OkiStuff Tags: #bitwise

NOT Operator

Bitwise NOT (also known as bitwise complement) is an operation which is effectively outputting the negative of the binary bit.

NOT 0011 (dec 3) = 1100 (dec 12) 

AND Operator

Bitwise AND is an operation that takes two binary representations of equal-length and performs a comparison. If both binary representations have a 1 in the same position, then the resulting bit in the output will be a 1. Else, it will be a 0

0011 (dec 3) AND 1100 (dec 12) = 0000 (dec 0)
0011 (dec 3) AND 0010 (dec 2) = 0010 (dec 2)

Any value that has been Bitwise NOT'd and then Bitwise AND'd should have an AND output of all 0.

OR Operator

Bitwise OR is an operation that takes two binary representations of equal-length and checks if any of the two binary representations has a value of 1 in any position. If true, the output binary will include a 1 in the position where the original 1(s) were found. If both binary representations have a 0 in the same position, that will result in a 0 at that position in the output binary.

0011 (dec 3) OR 1100 (dec 12) = 1111 (dec 15)

XOR Operator

Bitwise XOR is an operation that takes two binary representations of equal-length and checks if any of the two binary representations has a value of 1 in any position. However, Only one of the two binary representations can have a 1 in the same position. If both binary representations include a 1 in the same position, it will output as 0. Same as the Bitwise OR operator, if both binary representations have a 0 in the same position, that will also result in a 0 at that position in the output binary.

Due to the nature of the Bitwise XOR Operator, It is commonly used for many purposes in Computer Science. For example, using the XOR operator on a value against itself will always return an output of 0. Sometimes faster than loading a 0 value and saving it to a register.

0011 (dec 3) XOR 1100 (dec 12) = 1111 (dec 15)
1100 (dec 12) XOR 1010 (dec 10) = 0110 (dec 6)
1111 (dec 15) XOR 1111 (dec 15) = 0000 (dec 0)
0011 (dec 3) XOR 0011 (dec 3) = 0000 (dec 0)

Mathematical Equivalents

Taken from Wikipedia page for Bitwise Operations, Bitwise Operations Mathematical Equivalents of Bitwise Operations

Sources and Credits

This text is paraphrases and restates information found from the Wikipedia page for Bitwise Operations, Bitwise Operations

The Wikipedia article lists the following as it's sources and references:

Misc. references and Useful links are also available on the bottom of the Wikipedia Article

Copyright

(c) Frankie A. 2022, Licensed under the GNU GPL v3.0

This text is licensed under the GNU General Public License v3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment