- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Print binary values in Arduino
In order to print binary representation of numbers/ characters in Arduino, you can add 'BIN' as the second argument of your Serial.print() function. Example is shown below −
Example
void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); Serial.println(75); Serial.println(75, BIN); Serial.println('A'); Serial.println('A',BIN); Serial.println(1.912,BIN); } void loop() { // put your main code here, to run repeatedly: }
The Serial Monitor output for the above code can be seen below −
As you can see, this works only for integers and characters, and not for floating-point numbers. For characters, the binary value of the ASCII equivalent of that character is printed. You can access the ASCII table from here: https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html. For negative integers, the results are a bit strange. For instance, Serial.print(-75,BIN) will output 11111111111111111111111110110101, which translates to a decimal value of 4294967221, which corresponds to (2^32 – 75). So essentially, the value printed corresponds to an integer overflow. Of course, this output will be seen on a board which uses 4 bytes (32 bits) to represent an integer. If you are using a board which used two bytes (16 bits) to represent an integer, the output will be (2^16 – 75).
- Related Articles
- Print hexadecimal values in Arduino
- Print plain text in Arduino
- Traditional C formatting in Arduino print
- Print new line and tab in Arduino
- Print Binary Tree in C++
- Print Binary Tree in 2-Dimensions in C++
- How to export the binary file of a code in Arduino IDE
- Print Binary Tree levels in sorted order in C++
- Read values sent by Serial Monitor to Arduino
- Get max and min values of an array in Arduino
- Swift Program to Print Inverted Binary Pattern
- Print all full nodes in a Binary Tree in C++
- Print Common Nodes in Two Binary Search Trees in C++
- Print all odd nodes of Binary Search Tree in C++
- Print all internal nodes of a Binary tree in C++
