# What Are Prerequisites Before Embedded Programming?

## Following are the prerequisites that need to be covered before jumping into the sea of embedded programming.

1. Binary Numbering System

2. Bitwise operators

3. Unsafe Rust

4. Raw Pointers

5. 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.

 Decimal 0 1 2 4 13 21 27 45 Binary 0 1 10 100 1101 10101 11011 101101

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.

 Decimal Binary 0000 Starts at 0 0000 Starts at 0 0009 Goes up to 9 0001 Goes up to 1 0010 Reset the digit and increment the next digit. 0010 Reset the current digit and increment the next 0019 Again goes up to 9 0011 What happens next? 0099 What happens with 99, now 2 digits are at optimum 0100 Initial 2 digits got reset and the next digit will be incremented 0100 Both will get reset and the next digit will be incremented.

### Bitwise Operator

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.

### Unsafe Rust

What is that?

6. The hidden language inside Rust

7. 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

Unsafe Superpowers:

• 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.

### Raw pointer

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

### Static variables

• 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).