-
Notifications
You must be signed in to change notification settings - Fork 4
Programming in C
- O'Reilly Crash course on Embedded C Programming https://learning.oreilly.com/course/crash-course-on/9781836208099/
- O'Reilly Practical C++ https://learning.oreilly.com/course/practical-c/10000DIVC2022144/
We can organize eight bits into a single number, but these bits can also represent eight different things. For example, they can be wired to eight different LED lights. In fact, when you place values in special memory locations called memory-mapped I/O registers, these values turn on or off I/O pins. Since there are eight bits to the register’s byte, a single register can control eight LEDs. (Or, in our case, one LED and seven pins on which we can add more LEDs.)
This operators allow you to manipulate how each bit of your variable behaves.
Option | B8 | B7 | B6 | B5 | B4 | B3 | B2 | B1 | B0 |
---|---|---|---|---|---|---|---|---|---|
Var_A | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
Var_B | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
Reslt | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
Option | B8 | B7 | B6 | B5 | B4 | B3 | B2 | B1 | B0 |
---|---|---|---|---|---|---|---|---|---|
Var_A | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
Var_B | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
Reslt | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
Option | B8 | B7 | B6 | B5 | B4 | B3 | B2 | B1 | B0 |
---|---|---|---|---|---|---|---|---|---|
Var_A | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
Var_B | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
Reslt | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
Option | B8 | B7 | B6 | B5 | B4 | B3 | B2 | B1 | B0 |
---|---|---|---|---|---|---|---|---|---|
Var_A | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
Reslt | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 1 |
Variables: locations in the computer's memory which can be accessed by their identifier (their name). This way, the program does not need to care about the physical address of the data in memory
For a C++ program, the memory of a computer is like a succession of memory cells, each one byte in size, and each with a unique address. These single-byte memory cells are ordered in a way that allows data representations larger than one byte to occupy memory cells that have consecutive addresses.
When a variable is declared, the memory needed to store its value is assigned a specific location in memory (its memory address). Generally, C++ programs do not actively decide the exact memory addresses where its variables are stored.
The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as address-of operator.
foo = &myvar;
Pointers can be used to access the variable they point to directly. This is done by preceding the pointer name with the dereference operator (*).
myvar = 25;
foo = &myvar;
bar = *foo;
Due to the ability of a pointer to directly refer to the value that it points to, a pointer has different properties when it points to a char than when it points to an int or a float. Once dereferenced, the type needs to be known. And for that, the declaration of a pointer needs to include the data type the pointer is going to point to.
The declaration of pointers follows this syntax:
type * name;
Structures are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure.
We specify its member variables along with their datatype.
The above syntax is also called a structure template or structure prototype and no memory is allocated to the structure in the declaration. To use structure in our program, we have to define its instance. We can do that by creating variables of the structure type.
We can access structure members by using the [( . ) dot operator.]
structure_name.member1; strcuture_name.member2;
struct Point { int x; int y; }; struct Point p = {0}; // Both x and y are initialized to 0
struct structure_name str; str.member1 = value1; str.member2 = value2; str.member3 = value3;
struct structure_name str = { value1, value2, value3 };
The typedef keyword is used to define an alias for the already existing datatype. In structures, we have to use the struct keyword along with the structure name to define the variables. Sometimes, this increases the length and complexity of the code. We can use the typedef to define some new shorter name for the structure.
A union is a user-defined type in which all members share the same memory location. This definition means that at any given time, a union can contain no more than one object from its list of members.
An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration tag. And, it defines the set of named integer identifiers (called the enumeration set, enumerator constants, enumerators, or members). A variable of the enumeration type stores one of the values of the enumeration set defined by that type.
With ❤️ from 🇲🇽