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
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)
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
.
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)
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 a0
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)
Taken from Wikipedia page for Bitwise Operations, Bitwise Operations
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:
- CMicrotek Low-power Design Blog. CMicrotek. Retrieved 2015-08-12 (Possibly dead link)
- JTC1/SC22/WG14 N843 "C programming language", section 6.5.7
- Arithmetic operators - cppreference.com. en.cppreference.com. Retrieved 2016-07-06
- "INT13-C. Use bitwise operators only on unsigned operands". CERT: Secure Coding Standards. Software Engineering Institute, Carnegie Mellon University. Retrieved 2015-09-07.
- Operator (C# Reference)". Microsoft. Retrieved 2013-07-14.
- "Near constant time rotate that does not violate the standards?". Stack Exchange Network. Retrieved 2015-08-12.
- "Poor optimization of portable rotate idiom". GNU GCC Project. Retrieved 2015-08-11.
- "Circular rotate that does not violate C/C++ standard?". Intel Developer Forums. Retrieved 2015-08-12.
- "Constant not propagated into inline assembly, results in "constraint 'I' expects an integer constant expression"". LLVM Project. Retrieved 2015-08-11.
- The Java Language Specification, section 15.19. Shift Operators
- "Chapter 15. Expressions". oracle.com.
- "JavaScript Bitwise". W3Schools.com.
- "Synthesizing arithmetic operations using bit-shifting tricks". Bisqwit.iki.fi. 2014-02-15. Retrieved 2014-03-08.
Misc. references and Useful links are also available on the bottom of the Wikipedia Article
(c) Frankie A. 2022, Licensed under the GNU GPL v3.0
This text is licensed under the GNU General Public License v3.0