0% found this document useful (0 votes)
53 views17 pages

C For Embedded Systems

The document discusses data types and bitwise operations in C for embedded systems. It covers: 1) Different data types in C and their sizes and ranges, and why choosing the right data type matters for performance, avoiding overflow, and coercion. 2) Bitwise operators like AND, OR, XOR and how to use them to set, clear and test bits. 3) Shift operators and how to use them to generate masks and set values in multi-bit fields. 4) Compound operators that can be used with bitwise operations for tasks like hardware register access.

Uploaded by

MassimoMori
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views17 pages

C For Embedded Systems

The document discusses data types and bitwise operations in C for embedded systems. It covers: 1) Different data types in C and their sizes and ranges, and why choosing the right data type matters for performance, avoiding overflow, and coercion. 2) Bitwise operators like AND, OR, XOR and how to use them to set, clear and test bits. 3) Shift operators and how to use them to generate masks and set values in multi-bit fields. 4) Compound operators that can be used with bitwise operations for tasks like hardware register access.

Uploaded by

MassimoMori
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter 1

C for Embedded Systems

1
Chapter Review

• Section 1.1: C Data types for Embedded Systems


• Section 1.2: Bit-wise Operations in C

2
Sizes of Data Types

• three methods to find out the exact sizes of the data types:
1. Read the compiler manual.
2. Use pseudo function sizeof().
3. Use C99 data types.

3
Why should I care about which data type to
use?

• Performance
• Overflow
• coercion

4
Performance

• Using 64-bit data type for saving 32-bit variable will cause:
1. Waste RAM resource
2. Twice RAM access time
3. Additional arithmetic instructions

5
ANSI C (ISO C89) integer data types and their
ranges
Data type Size Range Min Range Max
char 1 byte -128 127
unsigned char 1 byte 0 255
short int 2 bytes -32,768 32,767
unsigned short int 2 bytes 0 65,535
int 4 bytes -2,147,483,648 2,147,483,647
unsigned int 4 bytes 0 4,294,967,295
long 4 bytes -2,147,483,648 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
long long 8 bytes -9,223,372,036,854,775,808 9,223,372,036,854,775,807
unsigned long long 8 bytes 0 18,446,744,073,709,551,615
6
Overflow

• Unlike assembly language programming, high level language programs do


not provide indications when overflow occurs and the program just fails
silently.
• If you use a short int to hold the number of seconds of a day, the
second count will overflow from 32,767 to -32,768. Even if your program
handles negative second count, the time will jump back to the day before.

7
Coercion

• If you write a statement with different operand data types for a binary
operation, the compiler will convert the smaller data type to the bigger data
type. These implicit data type is called coercion.
• The compiler may or may not give you warning when coercion occurs.
• If the variable is signed and the data sized is increased, the new bits are filled
with the sign bit (most significant bit) of the original value.
• When you assign a larger data type to a smaller data type variable, the
higher order bits will be truncated. 8
ISO C99 integer data types and their ranges

Data type Size Range Min Range Max


int8_t 1 byte -128 127
uint8_t 1 byte 0 to 255
int16_t 2 bytes -32,768 32,767
uint16_t 2 bytes 0 65,535
int32_t 4 bytes -2,147,483,648 2,147,483,647
uint32_t 4 bytes 0 4,294,967,295
int64_t 8 bytes -9,223,372,036,854,775,808 9,223,372,036,854,775,807
uint64_t 8 bytes 0 18,446,744,073,709,551,615

9
Bit-wise operators in C

A B AND OR EX-OR Invert


(A & B) (A | B) (A^B) ~B
0 0 0 0 0 1
0 1 0 1 1 0
1 0 0 1 1 1
1 1 1 1 0 0

10
Setting and Clearing (masking) bits

• Anything ORed with a 1 results in a 1; anything ORed with a 0 results in no


change.
• Anything ANDed with a 1 results in no change; anything ANDed with a 0
results in a zero.
• Anything EX-ORed with a 1 results in the complement; anything EX-ORed
with a 0 results in no change.

11
Testing bit with bit-wise operators in C

• When it is necessary to test a given bit to see if it is high or low, the unused
bits are masked and then the remaining data is tested.
• Example:
if (var1 & 0x20)

12
Bit-wise shift operation in C

Operation Symbol Format of Shift Operation


Shift Right >>  data >> number of bit-positions to be shifted right
Shift Left <<  data << number of bit-positions to be shifted left

13
Compound Operators
Instruction Its equivalent using
compound operators
a = a + 6; a += 6;
a = a – 23; a –= 23;
y = y * z; y *= z;
z = z / 25; z /= 25;
w = w | 0x20; w |= 0x20;
v = v & mask; v &= mask;
m = m ^ togBits; m ^= togBits;
14
Bit-wise operations using compound operators

• The majority of hardware access level code involves setting a bit or bits in a
register, clearing a bit or bits in a register, toggling a bit or bits in a register,
and monitoring the status bits. For the first three cases, the compound
operators are very suitable.

15
Using shift operator to generate mask

• One way to ease the generation of the mask is to use the left shift operator.
To generate a mask with bit n set to 1, use the expression: 1 << n
• If more bits are to be set in the mask, they can be “or” together. To generate
a mask with bit n and bit m set to 1, use the expression:
(1 << n) | (1 << m)
• register |= (1 << 6) | (1 << 1);

16
Setting the value in a multi-bit field

• register|= 1 << 30;


register &= ~(1 << 29);
register |= 1 << 28;
• register&= ~(7 << 28);
register |= 5 << 28;
• register = register & ~(7 << 28) | (5 << 28);

17

You might also like