Following are the prerequisites that need to be covered before jumping into the sea of embedded programming.
Binary Numbering System
Mutable static variables
Binary Number System
Keep the points in mind in sequence. Binary numbers are made up of only 1’s and 0’s. They don’t contain any other number we usually see in decimals (i.e. 2,3,4,…,9). Here are a few examples.
Binary numbers have a base of 2 in contrast to our decimal numbers which have a base of 10. What base tells us? It tells how many combinations can a number give us.
Starts at 0
Starts at 0
Goes up to 9
Goes up to 1
Reset the digit and increment the next digit.
Reset the current digit and increment the next
Again goes up to 9
What happens next?
What happens with 99, now 2 digits are at optimum
Initial 2 digits got reset and the next digit will be
Both will get reset and the next digit will be
Wondering! What these are? Now we are familiar with binary numbers. Next, we will see the concept of Bitwise operators that are based on binary numbers. There are a variety of operators in programming languages (i.e. arithmetic, relational, logical, and assignment operators). Bitwise operator is one of them.
Bitwise AND ( & )
It does a Boolean AND operation on each bit of its integer arguments.
Bitwise OR ( | )
This performs a Boolean OR operation on each bit of its integer arguments.
Bitwise XOR ( ^ )
It does a Boolean exclusive OR operation on each bit of its integer arguments.
Bitwise NOT ( ! )
This is a unary operator and operates by reversing all the bits in the operand.
Bitwise left shift ( << )
It moves all the bits in its first operand to the left by the number of places specified in the second operand.
Bitwise right shift ( >> )
Binary Right Shift Operator.
What is that?
The hidden language inside Rust
Gives us extra superpowers
Why is it?
It let us do the operations which compiler restricts us from
Without unsafe Rust, we can’t do operations on the hardware level
De-reference a raw pointer
Call an unsafe function or method
Access or modify a mutable static variable
Implement an unsafe trait
Keep a few things in mind while using unsafe Rust :
Unsafe rust doesn’t disable borrow checker or any other safety feature.
It gives you only access to these four features and the compiler doesn’t check for memory safety in them.
Inside an unsafe block, the code doesn’t need to be necessarily dangerous.
While writing code keeps the unsafe block smaller.
For the isolation of unsafe code, it’s better to enclose unsafe code within a safe abstraction. Wrapper!
When using unsafe rust we as programmers have to ensure that you are accessing memory in a valid way.
There are two new types of Unsafe Rus called raw pointers that are similar to references. They can be immutable or mutable and can be written as:
*const T (Immutable)
*mut T (Mutable)
Note: Asterisk “ * ” is not a de-referencing operator, it’s part of a type name. Let’s see how to create an immutable and mutable raw pointer from reference. let mut num = 5;
let r1=&num as *const i32; let r2=&mut num as *mut i32;
Is something wrong! We didn’t include the unsafe keyword here? Well, it’s okay to create raw pointers outside unsafe but it’s must include them while de-referencing them.
How raw pointers are different from references!
Are allowed to ignore the borrowing rules by having both immutable and mutable pointers or multiple mutable pointers to the same location
Aren’t guaranteed to point to valid memory
Are allowed to be null
Don’t implement any automatic cleanup
Global variables in Rust are called static variables
Static variables are similar to Constants
The naming style of static variables is in SCREAMING_SNAKE_CASE
Type annotation for static variables is a must (like constants).
Rust allows access to read-only static variables (immutable static variables ), however doesn’t allow access to immutable static variables.
Static variables always have a fixed address in memory.