Bit Manipulation in C

Similar documents
Fundamentals of Programming

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

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

Fundamental of Programming (C)

CS 253. January 14, 2017

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

More on C programming

A Fast Review of C Essentials Part I

Programming refresher and intro to C programming

Beginning C Programming for Engineers

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CSE I-Sem)

Programming. Elementary Concepts

DEPARTMENT OF MATHS, MJ COLLEGE

Lecture 3. More About C

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

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Arithmetic Operators. Portability: Printing Numbers

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Computer Organization & Systems Exam I Example Questions

Chapter 7. Basic Types

AGENDA Binary Operations CS 3330 Samira Khan

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

MCA Semester 1. MC0061 Computer Programming C Language 4 Credits Assignment: Set 1 (40 Marks)

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

The C language. Introductory course #1

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Advanced C Programming Topics

EL2310 Scientific Programming

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

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Logical and Bitwise Expressions

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

COMP1917 Computing 1 Written Exam Sample Questions

Arithmetic and Bitwise Operations on Binary Data

Lecture 02 C FUNDAMENTALS

Quick Reference Guide

C - Basics, Bitwise Operator. Zhaoguo Wang

Computers Programming Course 6. Iulian Năstac

SECTION II: LANGUAGE BASICS

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

1.3b Type Conversion

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

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

C: How to Program. Week /Mar/05

Logical and Bitwise Expressions

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

This is CS50. Harvard University Fall Quiz 0 Answer Key

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Arithmetic and Bitwise Operations on Binary Data

JAVA OPERATORS GENERAL

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Computers Programming Course 5. Iulian Năstac

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

C++ Structures Programming Workshop 2 (CSCI 1061U)

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

ECET 264 C Programming Language with Applications

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

C Programming Language (Chapter 2 of K&R) Variables and Constants

OBJECT ORIENTED PROGRAMMING

Computer System and programming in C

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Introduction to Computing Systems Fall Lab # 3

Compiling and Running a C Program in Unix

The Design of C: A Rational Reconstruction

BSM540 Basics of C Language

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Input/Output Week 5:Lesson 16.1

CSCI2467: Systems Programming Concepts

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

A few examples are below. Checking your results in decimal is the easiest way to verify you ve got it correct.

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

These are reserved words of the C language. For example int, float, if, else, for, while etc.

ECET 264 C Programming Language with Applications

The Design of C: A Rational Reconstruction"

CS61, Fall 2012 Midterm Review Section

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

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

Course Outline Introduction to C-Programming

Kurt Schmidt. October 30, 2018

CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

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

Work relative to other classes

Applied Computer Programming

6.096 Introduction to C++ January (IAP) 2009

Procedural programming with C

Reserved Words and Identifiers

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Programming in C - Part 2

Transcription:

Bit Manipulation in C Bit Manipulation in C C provides six bitwise operators for bit manipulation. These operators act on integral operands (char, short, int and long) represented as a string of binary digits. Bitwise Operators logical operators: (unary) bitwise complement: ~ bitwise and: & &= bitwise or: = bitwise exclusive or: ^ ^= shift operators: left shift: << <<= right shift: >> >>= ICT106_Prac_Week_6 1 ICT106_Prac_Week_6 2 x y ~x x & y x y x ^ y 0 0 1 0 0 0 0 1 1 0 1 1 1 0 0 0 1 1 1 1 0 1 1 0 Examples of application of bitwise operators on 8 bit representations of integers x and y int x, y, result; x = 15; /* 00001111 */ y = -10; /* 11110110 */ result = x & y; /* result now has the value decimal 6, i.e., 00000110 */ ICT106_Prac_Week_6 3 ICT106_Prac_Week_6 4 Expression Representation Value x 00001111 15 y 11110110-10 result = x & y; 00000110 6 result = x y; 11111111-1 result = x ^ y; 11111001-7 result = ~(x ^ y); 00000110 6 result = ~x; 11110000-16 result = x << 2; 00111100 60 result = x >> 2; 00000011 3 The bitwise AND operator & is often used to mask off some set of bits, e.g., x = x & 017; /* N.B. 017 is an octal value */ sets to zero all but the low-order 4 bits of x. The bitwise OR operator is often used to turn bits on, e.g., x = x y; sets to one in x the bits that are set to one in y. ICT106_Prac_Week_6 5 ICT106_Prac_Week_6 6 1

The bitwise exclusive OR operator ^ sets a one in each bit position where its operands have different bits, and zero where they are the same. The bitwise complement operator ~, also called the one s complement operator, inverts the bit string it is applied to. The 0s become 1s and the 1s become 0s (Toggle) The shift operators << and >> perform left and right shifts of their left operand by the number of bit positions given by the right operand, which must be positive. Structures in C ICT106_Prac_Week_6 7 ICT106_Prac_Week_6 8 Struct-ure Structs are like records and can be used to group data elements of different types, i.e., a struct is (usually) a collection of mixed type items. Two ways to define a structure: struct struct_name {variables typedef struct {variables Type_name; The typedef keyword allows to create name for a type, e.g. typedef unsigned char BYTE; BYTE one, bytes[5], *pr; ICT106_Prac_Week_6 9 ICT106_Prac_Week_6 10 char surname[4]; char firstinit; typedef struct { char surname[4]; char firstinit; EMPLOYEE; To Declare struct variables This defines a new data type called 'Employee'. This definition is for the use of C compiler, it does not allocate any space for the component variables. This definition can be used to declare variables as follows: Struct Employee manager; struct Employee worker1; EMPLOYEE manager; EMPLOYEE worker1; ICT106_Prac_Week_6 11 ICT106_Prac_Week_6 12 2

Accessing elements in a struct Initialising a struct is similar to that used for arrays: struct employee worker2 = { John, B, 32, 45800.15 Memory allocation for a structure J o h n B 32 45800.15 Char[0] surname Char[3] Initial Age Wage ICT106_Prac_Week_6 13 The elements (members) of a struct are accessed using the dot notation: worker1.firstinit = 'J'; worker1.age = 22; worker1.wage = 30000.00; manager.wage = 2* worker1.wage; ICT106_Prac_Week_6 14 Bit Field Another method of manipulating bits is to use a bit field; A bit field is constructed with a structure declaration that labels each field and determines its width; For example, to declare a Boolean data type #define TRUE 1 #define FALSE 0 struct BOOL { unsigned int bit : 1; ICT106_Prac_Week_6 15 ICT106_Prac_Week_6 16 Flag Register This definition creates a new data type BOOL that contains one 1-bit field. struct BOOL flag; flag.bit = TRUE; flag.bit = FALSE; struct FLAG { unsigned int overflow : 1; unsigned int sign : 1; unsigned int zero : 1; unsigned int aux_carry : 1; unsigned int parity : 1; unsigned int carry : 1; unsigned int direc : 1; unsigned int interrupt : 1; ICT106_Prac_Week_6 17 ICT106_Prac_Week_6 18 3

Nested Structures A struct can contain or nest another struct; Nested structures are often used to construct complex data record; For example, we can create a structure called names to represent an employee s name struct names { char first[4]; char surname[4]; struct names name; // nested struct int age; ICT106_Prac_Week_6 19 ICT106_Prac_Week_6 20 Pointers to Structures Again, the elements of a struct can be accessed using the dot notation: struct Employee worker1; strcpy((worker1.name).last, Bob ); strcpy((worker1.name).surname, John ); worker1.age = 43; The construction is interpreted from left to right Using pointers is easier to manipulate structures than the arrays; To overcome the problem of using structures as function arguments in some older C implementations; Enable to create dynamic size arrays ICT106_Prac_Week_6 21 ICT106_Prac_Week_6 22 /* employee record: demonstrating use of pointer to a structure */ #include <stdio.h> #define LEN 11 // define a constant LEN struct names { char last[len]; char surname[len] struct names name; // nested structure ICT106_Prac_Week_6 23 int main() { struct Employee workers[2] = { { { Ewen, Villard, 23, 34000.00, { { Rodney, Swillbelly, 41, 58000.00 ICT106_Prac_Week_6 24 4

struct Employee *him; // a pointer to a structure him = &workers[0]; // assign address to pointer printf("workers[0].name.surname is %s\n", workers[0].name.surname); printf( workers[0].age is %d\n, workers[0].age); printf( (*him).age is %d\n, (*him).age); printf( him->age is %d\n, him->age); him++; // point to the next structure printf("him->name.surname is %s\nhim->wage is %.2f\n", him->name.surname, him->wage); return 0; workers[0].name.surname is Villard workers[0].age is 23 (*him).age is 23 him->age is 23 him->name.surname is Swillbelly him->wage is 58000.00 ICT106_Prac_Week_6 25 ICT106_Prac_Week_6 26 Linked List Linked lists contain a set of (mostly) similar data structures; An item can be added to or removed from a linked list and that the length of linked lists can be dynamically changed; The length of arrays is always fixed and it must be defined before use; Construction of an employee linked list is shown below: strut Employee *next; // pointer to the next element strut names name; ICT106_Prac_Week_6 27 ICT106_Prac_Week_6 28 int main() { struct Employee workers[2] = { { NULL, // pointer to nowhere {"Ewen", "Villard", 23, 34000.00, { NULL, // pointer to nowhere {"Rodney", "Swillbelly", 41, 58000.00 ICT106_Prac_Week_6 29 struct Employee *him; // a pointer to a structure him = &workers[0]; // assign address to pointer him->next = him+1; // link to the next record printf("(*(him->next)).name.surname is %s\n", (*(him->next)).name.surname); him++; // point to the next structure printf("him->name.surname is %s\n, him->name.surname); ICT106_Prac_Week_6 30 5

(*(him->next)).name.surname is Swillbelly him->name.surname is Swillbelly struct Employee * age wage struct Employee * age wage NULL ICT106_Prac_Week_6 31 /* Book record: demonstrating linked list */ #include <stdio.h> #include <stdlib.h> struct Book { struct Book *next; // pointers to next struct in list int barcode; float price; int main() { struct Book *head = NULL; // head of list struct Book *prev, *current; int i=1; ICT106_Prac_Week_6 32 do { /* create memory block for Book structure */ current = (struct Book *) malloc(sizeof(struct Book)); if(head==null) { head = current; // first structure else { prev->next = current; // subsequent structure printf("\nenter barcode for %d book: ", i); scanf("%d%*c", &current->barcode); printf("enter price for %d book: ", i); scanf("%f%*c", &current->price); current = head; // pointer to head of list while(current!=null) { printf("book: %d Price: %f\n", current->barcode, current->price); /* pointer to subsequent struct */ current = current->next; current->next = NULL; // set the next struct to NULL prev = current; // update prev pointer i++; while (i<5); return 0; ICT106_Prac_Week_6 33 ICT106_Prac_Week_6 34 6