Working on...

your queries, use contact form

28 Aug 2020

C - Operators and data types used for programming of microcontroller 8051

 

C - Operators and data types used for programming of microcontroller 8051

 

C - Operators

Operator: Operator is symbol which is used to in arithmetic and logical operation to perform particular task. Following are some types of operators used in c-language.

  • Arithmetic operators
  • Logical operators
  • Relational operators
  •  Arithmetic operators: Arithmetic operations like addition, subtraction, multiplication, division, increment and decrement are possible using arithmetic operators. Following table shows symbol of operator, its working and examples.

            Lets consider contents of A=12H and B=24H for all following examples

Operator symbol

Description

Example

+

Addition operation

A+B (Answer is 36)

-

Subtract operation

B-A (Answer is 12)

*

Multiply operation

A*B (Answer is 288)

/

Division operation

B/A (Answer is 2)

++

Increment operation

B++ (Answer is 25)

--

Decrement operation

B-- (Answer is 23)

 

  • Logical operators (Bitwise): Logical operations like NOT (compliment), AND, OR, XOR, left shift and right shift are possible using logical operators. Following table shows symbol of operator, its working and examples.

            Lets consider contents of A=12H and B=24H for all following examples

Operator symbol

Description

Example

~

Binary NOT (1’s complement) operation

~A (Answer is 36)

&

Binary AND operation

A & B (Answer is 0)

|

Binary OR operation

A | B (Answer is 36)

^

Binary XOR operation

A ^ B (Answer is 36)

<< 

Binary Left shift (by one) operation

B << 1 (Answer is 48)

>> 

Binary Right shift (by one) operation

B >> 1 (Answer is 12)

 

To cross check logical operations, have a look at truth tables of NOT gate, AND gate, OR gate and XOR gate.

NOT

 

AND

 

OR

 

XOR

A

B

Y

A

B

Y

A

B

Y

A

B

Y

0

-

1

0

0

0

0

0

0

0

0

0

1

-

0

0

1

0

0

1

1

0

1

1

 

1

0

0

1

0

1

1

0

1

1

1

1

1

1

1

1

1

0

 

 

 

  • Relational operations:  Relational operations like equals to, not equals to, greater than, less than, less than or equals to and greater than or equals to are possible using logical operators. Following table shows symbol of operator, its working and examples.

            Lets consider contents of A=12H and B=24H for all following examples

Operator symbol

Description

Example

= =

Check values of A and B are equal or not

A= =B

!=

Check values of A and B are equal or not

A != B

> 

Greater than operation

A > B

< 

Less than operation

A < B

>=

Greater than or equals to operation

A >= B

<=

Less than or equals to operation

A <= B

 

 

Data Types

  • Data types: In case of assembly language programming, only 8-bit data type is used. If data is greater than 8-bit then it can break down into 8-bit data for processing. In case of C- language programming, there are variable data types that can be from 1-bit to 64-bit. Data types are used to define/declaring functions or variables. Data types can be positive or negative i.e. signed or unsigned. Following table shows some C data types along with their size or range

Data type

Bits

Size (Range)

bit

1

0 to 1

sbit

1

0 to 1

sfr

8

0 to 255 (RAM addresses from 80H-FF H only)

unsigned char

8

0 to 255

signed char

8

-128 to +127

unsigned int

16

0 to 65535

Sf1

16

0 to 65535

unsigned short

16

0 to 65535

signed int

16

-32768 to +32767

signed short

16

-32768 to +32767

enum

16

-32768 to +32767

unsigned long

32

0 to 4294697295

singed long

32

-2147483648 to +2147483647

No comments:

Post a Comment