- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bitwise NOT in Arduino
Unlike logical NOT, which inverts the truth value of an expression, the bitwise NOT applies to each bit of a number and inverts its value (0 to 1 and 1 to 0). The operator is ~.
The syntax thus is ~a, where a is the number on which this operator has to apply.
Please note, that all the leading 0s in the number’s representation are also converted to 1. For example, if your board uses 16 bits to represent an integer, then here’s what ~10 will look like
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 10 | |
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 0 | 1 | ~10=-11 |
As you can see, each bit of 10 got inverted. This number corresponds to, using 2’s complement, -11. You can use this site for quickly converting decimal number to their 2’s complements and vice versa https://www.exploringbinary.com/twos-complement-converter/.
You can read more about 2’s complement here &minnus; https://www.tutorialspoint.com/two-scomplement
Example
Let’s verify this on the Serial Monitor. The code is given below −
void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); int a = 10; Serial.println(~a); } void loop() { // put your main code here, to run repeatedly: }
Output
The Serial Monitor output is −
As you can see, the output was exactly as expected.
- Related Articles
- Bitwise XOR in Arduino
- Bitwise AND and OR in Arduino
- Bitwise Right/ left shift numbers in Arduino
- Logical NOT in Arduino\n
- What is Bitwise NOT Operator (~) in JavaScript?
- What is JavaScript Bitwise NOT(~) Operator?
- Explain JavaScript Bitwise NOT, Left shift and Right shift?
- Check if two strings are equal or not in Arduino
- Check if the board is connected or not in Arduino IDE
- How to perform Bitwise Not operation on images using Java OpenCV?
- OpenCV Python – How to perform bitwise NOT operation on an image?
- How to compute bitwise AND, OR and NOT of given input tensors in PyTorch?
- Find subsequences with maximum Bitwise AND and Bitwise OR in Python
- Bitwise Operators in C
- Bitwise Operators in C++
