Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

Similar documents
OBJECT ORIENTED PROGRAMMING USING C++

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

Chapter 8 STRUCTURES IN C

Type Checking. Prof. James L. Frankel Harvard University

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Programming for Engineers Structures, Unions

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

A flow chart is a graphical or symbolic representation of a process.

Fundamentals of Programming Session 24

EL6483: Brief Overview of C Programming Language

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

AGENDA Binary Operations CS 3330 Samira Khan

Computer System and programming in C

Bit Manipulation in C

DEPARTMENT OF MATHS, MJ COLLEGE

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CSE 303 Lecture 18. Bitwise operations. reading: Programming in C Ch. 12. slides created by Marty Stepp

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Fundamental of Programming (C)

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

High Performance Computing in C and C++

Reserved Words and Identifiers

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Expressions and Precedence. Last updated 12/10/18

Data Representation and Storage. Some definitions (in C)

Everything Else C Programming and Software Tools. N.C. State Department of Computer Science

Hardware: Logical View

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$12$

CS429: Computer Organization and Architecture

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

12 CREATING NEW TYPES

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Fundamentals of Programming

We would like to find the value of the right-hand side of this equation. We know that

Computers Programming Course 6. Iulian Năstac

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Data III & Integers I

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Low-Level Programming

Computers Programming Course 5. Iulian Năstac

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

C Structures, Unions, Bit Manipulations, and Enumerations

Bitwise Data Manipulation. Bitwise operations More on integers

STRUCTURES & FILE IO

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Data III & Integers I

Chapter 10 C Structures, Unions, Bit Manipulations

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Fundamental of Programming (C)

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

CSE 230 Intermediate Programming in C and C++

Programming. Elementary Concepts

C++ Programming Chapter 7 Pointers

Exercise: Using Numbers

Data Structures Unit 02

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Review of the C Programming Language for Principles of Operating Systems

Informatics Ingeniería en Electrónica y Automática Industrial

CS201 - Introduction to Programming Glossary By

MYcsvtu Notes LECTURE 34. POINTERS

Bits, Bytes, and Integers

Bits, Bytes and Integers

JAVA OPERATORS GENERAL

Programming. Structures, enums and unions

Variables. Data Types.

reply db y prompt db Enter your favourite colour:, 0 colour db 80 dup(?) i db 20 k db? num dw 4000 large dd 50000

CS 253. January 14, 2017

Computer Organization & Systems Exam I Example Questions

Data III & Integers I

C Language Advanced Concepts. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Logical and Bitwise Expressions

Integer Representation

Applied Computer Programming

by Pearson Education, Inc. All Rights Reserved.

EL2310 Scientific Programming

Data Representation and Storage

Short Notes of CS201

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CS Programming In C

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

Review of the C Programming Language

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

CSE I-Sem)

CPS 104 Computer Organization and Programming

.Net Technologies. Components of.net Framework

UNIT 3 OPERATORS. [Marks- 12]

Binary representation of integer numbers Operations on bits

Arithmetic and Bitwise Operations on Binary Data

Structures, Operators

SIGNED AND UNSIGNED SYSTEMS

Tutorial 1: C-Language

Motivation was to facilitate development of systems software, especially OS development.

Bitwise Operator and Typecasting

Transcription:

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

Structures Unions Endianness Bit field Bit manipulation

Collections of related variables (aggregates) under one name Can contain variables of different data types Commonly used to define records to be stored in files Combined with pointers, can create linked lists, stacks, queues, and trees Can hold the data associated to a hardware device

Example struct card { }; char *face; char *suit; struct introduces the definition for structure card card is the structure name and is used to declare variables of the structure type card contains two members of type char * These members are face and suit

struct information A struct cannot contain an instance of itself Can contain a member that is a pointer to the same structure type A structure definition does not reserve space in memory Instead creates a new data type used to define structure variables Definitions Defined like other variables: card onecard, deck[ 52 ], *cptr; Can use a comma separated list: struct card { char *face; char *suit; } onecard, deck[ 52 ], *cptr;

Valid operations Assigning a structure to a structure of the same type Taking the address (&) of a structure Accessing the members of a structure Using the sizeof operator to determine the size of a structure

Initializer lists Example: card onecard = { "Three", "Hearts" }; Assignment statements Example: card threehearts = onecard; Could also define and initialize threehearts as follows: card threehearts; threehearts.face = Three ; threehearts.suit = Hearts ;

Accessing structure members Dot operator (.) used with structure variables card mycard; printf( "%s", mycard.suit ); Arrow operator (->) used with pointers to structure variables card *mycardptr = &mycard; printf( "%s", mycardptr->suit ); mycardptr->suit is equivalent to ( *mycardptr ).suit

Passing structures to functions Pass entire structure Or, pass individual members Both pass call by value To pass structures call-by-reference Pass its address Pass reference to it To pass arrays call-by-value Create a structure with the array as a member Pass the structure

typedef Creates synonyms (aliases) for previously defined data types Use typedef to create shorter type names Example: typedef struct Card *CardPtr; Defines a new type name CardPtr as a synonym for type struct Card * typedef does not create a new data type Only creates an alias

unsigned long int count ; versus typedef unsigned long int DWORD32 ; DWORD32 count ;

typedef unsigned char BYTE8 ; typedef unsigned short int WORD16 ; typedef unsigned long int DWORD32 ; typedef int BOOL ; #define FALSE 0 #define TRUE 1

Structures Unions Endianness Bitfield Bit manipulation

union Memory that contains a variety of objects over time Only contains one data member at a time Members of a union share space Conserves storage Only the last data member defined can be accessed union definitions Same as struct union Number { int x; float y; }; union Number value;

union Data { int i; float f; char str[20]; } data; The memory occupied by a union will be large enough to hold the largest member of the union. For example, in above example the type data will occupy 20 bytes. 16 March 14, 2017

Valid union operations Assignment to union of same type: = Taking address: & Accessing union members:. Accessing members using pointers: ->

Given an address, we can cast it as a pointer to data of the desired type, then deference the pointer by subscripting. Without knowing the data type used in its declaration, we can read or write various parts of an object named operand using: ((BYTE8 *) &operand)[k]

typedef struct KYBD_INFO { BYTE8 lo ; BYTE8 hi ; WORD16 both ; } KYBD_INFO ; BOOL Kybd_Flags_Changed(KYBD_INFO *kybd) {... kybd->both = ((WORD16 *) &new_flags)[0] ; kybd->lo = ((BYTE8 *) & new_flags)[0] ; kybd->hi = ((BYTE8 *) &new_flags)[1] ; if (kybd->both == old_flags) return FALSE ; old_flags = kybd->both ; return TRUE ; }

union { 31 unsigned long dd ; dd unsigned short dw[2] ; dw[1] dw[0 unsigned char db[4] ; db[3] db[2] db[1] d

typedef union VARIANT { BYTE8 b[2] ; WORD16 w ; } VARIANT ; BOOL Kybd_Flags_Changed(KYBD_INFO *kybd) { static WORD16 old_flags = 0xFFFF ; VARIANT *flags = (VARIANT *) malloc(sizeof(variant)) ; dosmemget(0x417, sizeof(variant), (void *) flags) ; status->both = flags->w ; status->lo = flags->b[0] ; status->hi = flags->b[1] ; free(flags) ; } if (status->both == old_flags) return FALSE ; old_flags = status->both ; return TRUE ;

Structures Unions Endianness Bitfield Bit manipulation

union { unsigned long dd; unsigned short dw[2]; unsigned char db[4]; }; Endianness differ depending on architecture. X86: little Motorola, sparc: big 31...0 db[3] dw[1] db[2] dd db[1] dw[0] db[0] 31...24 23...16 15...8 7...0 db[0] dw[0] Little endian vs Big endian 31...0 db[1] dd db[2] dw[1] db[3] 31...24 23...16 15...8 7...0 TDDI11, Embedded Software 24 March 14, 2017

Big-endian systems are systems in which the most significant byte of the word is stored in the smallest address given and the least significant byte is stored in the largest. In contrast, little endian systems are those in which the least significant byte is stored in the smallest address.............

Think about communication between two machine that have different Endianness One machine writes integers to a file and another machine with opposite Endianness reads it. Sending numbers over network between two machines with different Endianess. Think about serial communication when we split the data into multiple chunks!!

Structures Unions Endianness Bit fields Bit manipulation

In embedded systems, storage is at a premium It may be necessary to pack several objects into one word Bit fields allow single bit objects They must be part of a structure

Embedded systems must communicate with peripherals at low-level. A register of a disk controller, for example, has several fields. How can we represent this memory compactly?

struct DISK_REGISTER { unsigned int ready:1; unsigned int error_occured:1; unsigned int disk_spinning:1; unsigned int write_protect:1; unsigned int head_loaded:1; unsigned int error_code:8; unsigned int track:9; unsigned int sector:5; unsigned int command:5; }; Bit fields must be part of a structure/union stipulated by the C standard

Structures Unions Endianness Bitfield Bit manipulation

Operation Boolean Operator Bitwise Operator AND && & OR XOR unsupported ^ NOT! ~ Boolean operators are primarily used to form conditional expressions (as in an if statement) Bitwise operators are used to manipulate bits.

Most implementations of C don't provide a Boolean data type. Any numeric data type may be used as a Boolean operand. Boolean operators yield results of type int, with true and false represented by 1 and 0. Zero is interpreted as false; any non-zero value is interpreted as true.

(5!3) && 6 True / False? = (true OR (NOT true)) AND true = (true OR false) AND true = (true) AND true = true = 1

Bitwise operators operate on individual bit positions within the operands The result in any one bit position is entirely independent of all the other bit positions.

m p m AND p 0 0 0 1 0 0 1 0 0 1 1 p interpretation If bit m of the mask is 0, then bit p is cleared to 0. If bit m of the mask is 1, then bit p is unchanged.

m p m OR p interpretation 0 0 0 1 1 p 1 0 1 1 1 1 If bit m of the mask is 0, then bit p is unchanged. If bit m of the mask is 1, then bit p is set to 1.

m p m XOR p 0 0 0 1 1 p 1 0 1 1 0 ~p interpretation If bit m of the mask is 0, then bit p is unchanged. If bit m of the mask is 1, then bit p is inverted.

(5 ~3) & 6 = (00..0101 OR ~00..0011) AND 00..0110 = (00..0101 OR 11..1100) AND 00..0110 = (11..1101) AND 00..0110 = 00..0100 = 4

A 1 in the bit position of interest is AND'ed with the operand. The result is non-zero if and only if the bit of interest was 1: if ((bits & 64)!= 0) /* check to see if bit 6 is set */ b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 & 01000000 0b 6 000000

Since any non-zero value is interpreted as true, the redundant comparison to zero may be omitted, as in: if (bits & 64) /* check to see if bit 6 is set */

The mask (64) is often written in hex (0x0040), but a constantvalued shift expression provides a clearer indication of the bit position being tested: if (bits & (1 << 6)) /* check to see if bit 6 is set */ Almost all compilers will replace such constant-valued expressions by a single constant, so using this form almost never generates any additional code.

#define FALSE (0) #define TRUE (1) typedef unsigned char BOOL ; typedef struct SHIFTS { BOOL right_shift ; BOOL left_shift ; BOOL ctrl ; BOOL alt ; BOOL left_ctrl ; BOOL left_alt ; } SHIFTS ; BOOL Kybd_Flags_Changed(SHIFTS *) ; void Display_Kybd_Flags(SHIFTS *) ; void main() { SHIFTS kybd ; } do { /* repeat until both shift keys are pressed */ if (Kybd_Flags_Changed(&kybd)) Display_Kybd_Flags(&kybd) ; } while (!kybd.left_shift!kybd.right_shift) ;

typedef unsigned int WORD16 ; BOOL Kybd_Flags_Changed(SHIFTS *kybd) { static WORD16 old_flags = 0xFFFF ; WORD16 new_flags ; dosmemget(0x417, sizeof(new_flags), &new_flags) ; if (new_flags == old_flags) return FALSE ; old_flags = new_flags ; kybd->right_shift = (new_flags & (1 << 0))!= 0 ; kybd->left_shift = (new_flags & (1 << 1))!= 0 ; kybd->ctrl = (new_flags & (1 << 2))!= 0 ; kybd->alt = (new_flags & (1 << 3))!= 0 ; kybd->left_alt = (new_flags & (1 << 9))!= 0 ; kybd->left_ctrl = (new_flags & (1 << 8))!= 0 ; return TRUE ; }

Setting a bit to 1 is easily accomplished with the bitwise-or operator: bits = bits (1 << 7) ; /* sets bit 7 */ b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 10000000 1b 6 b 5 b 4 b 3 b 2 b 1 b 0 This would usually be written more succinctly as: bits = (1 << 7) ; /* sets bit 7 */

Note that we don't add (+) the bit to the operand! That only works if the current value of the target bit in the operand is known to be 0. Although the phrase "set a bit to 1" suggests that the bit was originally 0, most of the time the current value of the bit is actually unknown.

Clearing a bit to 0 is accomplished with the bitwise-and operator: bits &= ~(1 << 7) ; /* clears bit 7 */ (1 << 7) 10000000 ~(1 << 7) 01111111 Note that we don't subtract the bit from the operand!

When clearing bits, you have to be careful that the mask is as wide as the operand. For example, if bits is changed to a 32-bit data type, the right-hand side of the assignment must also be changed, as in: bits &= ~(1L << 7) ; /* clears bit 7 */

Inverting a bit (also known as toggling) is accomplished with the bitwise-xor operator as in: bits ^= (1 << 6) ; /* flips bit 6 */ Although adding 1 would invert the target bit, it may also propagate a carry that would modify more significant bits in the operand.

Extract minutes from time Bits 15-11 Bits 10-5 Bits 4-0 time Hours Minutes Seconds 2 Bits 15-11 Bits 10-6 Bits 5-0 time >> 5????? Hours Minutes Bits 15-11 Bits 10-6 Bits 5-0 (time >> 5) & 0x3F 00000 00000 Minutes minutes = (time >> 5) & 0x3F 15 0 Minutes

Updates minutes in time Bits 15-11 Bits 10-5 Bits 4-0 oldtime Hours Old Minutes Seconds 2 Bits 15-11 Bits 10-5 Bits 4-0 newtime = oldtime & ~(0x3F << 5) Hours 000000 Seconds 2 Bits 15-11 Bits 10-5 Bits 4-0 newtime = (newmins & 0x3F) << 5 Hours New Minutes Seconds 2