+ All Categories
Home > Documents > 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or"...

1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or"...

Date post: 21-Dec-2015
Category:
View: 229 times
Download: 0 times
Share this document with a friend
75
1 Bitwise Operators
Transcript
Page 1: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

1

Bitwise Operators

Page 2: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

2

Bits and Constants

Page 3: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

3

Bitwise Operators

Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement" operator ~

Page 4: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

4

Expression Representation Value

a 00000000 00000000 10000010 00110101 33333

b 11111111 11111110 11010000 00101111 -77777

a & b 00000000 00000000 10000000 00100101 32805

a ^ b 11111111 11111110 01010010 00011010 -110054

a | b 11111111 11111110 11010010 00111111 -77249

~ ( a | b ) 00000000 00000001 00101101 11000000 77248

~ a & ~ b 00000000 00000001 00101101 11000000 77248

Representations

• int a = 33333, b = -77777;

The value of each bit is determined only by the bit(s) in its position

Page 5: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

5

Expression Representation Action

c 00000000 00000000 00000000 01011010 unshifted

c << 4 00000000 00000000 00000101 10100000 left shifted 4

a 10000000 00000000 00000000 00000000 unshifted

a >> 3 11110000 00000000 00000000 00000000 right shifted 3

b 10000000 00000000 00000000 00000000 unshifted

b >> 3 00010000 00000000 00000000 00000000 right shifted 3

Representations

char c = 'Z';

int a = 1 << 31; /* shift 1 to the high bit */

unsigned b = 1 << 31;

For signed data types bit positions vacated by shifting is filled with a copy of the highest (sign) bit for signed data type

Page 6: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

6

Left Shifts

short a = 0x68ab;

...

a <<= 3; /* shift left 3 bits */

Same as: a = a << 3;

http://www.harpercollege.edu/bus-ss/cis/166/mmckenzi/lect19/l19e.htm

Bits positions vacated by shift are filled with zeros

Page 7: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

7

Right Shifts (Unsigned)

unsigned short a = 0x98ab;

. . .

a >>= 5; /* shift right 5 bits */

http://www.harpercollege.edu/bus-ss/cis/166/mmckenzi/lect19/l19e.htm

For unsigned data type, bits positions vacatedby shift are filled with zeros.

Page 8: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

8

Right Shifts (Signed)short a = 0x98ab;

. . .

a >>= 5; /* shift right 5 bits */

http://www.harpercollege.edu/bus-ss/cis/166/mmckenzi/lect19/l19e.htm

Bit positions vacated by shifting is filled with a copy of the highest (sign) bit for signed data type

Page 9: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

9

Right Shifts (Signed)short a = 0x78ab;

. . .

a >>= 5; /* shift right 5 bits */

http://www.harpercollege.edu/bus-ss/cis/166/mmckenzi/lect19/l19e.htm

Bit positions vacated by shifting is filled with a copy of the highest (sign) bit for signed data type

Page 10: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

10

Implementation Note x<<n is equivalent to multiplication by 2n.

x>>n is equal to x/2n

Shifting is much faster than actual multiplication (*) or division (/)==> Multiplications / divisions by powers of 2 should be implemented using shifts.

0x00010x00020x00040x00080x00100x00200x0040 0000000001000000

0000000000000001

0000000000000010

0000000000000100

0000000000001000

0000000000010000

0000000000100000

Page 11: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

11

int main(void){int num=0;do{

printf("Enter an integer:\n");scanf("%d", &num);bit_print(num);putchar('\n');

} while (num!=0);return 0;

}

Printing the bits of an integer

Prints the binary representation of an integer. E.g:00000000 00000000 00000000 00000111(MSB) (LSB)

Page 12: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

12

#include <limits.h>void bit_print(int a) { int i; int n = sizeof(int) * CHAR_BIT; /* #define CHAR_BIT 8 (in <limits.h>)*/ int mask = 1 << (n - 1); /* mask = 100...0 */

for (i = 1; i <= n; ++i) {

putchar(((a & mask) == 0) ? '0' : '1');

a <<= 1;

if (i % CHAR_BIT == 0 && i < n) putchar(' ');

}}

n is the number of bits in an integer

Prints the most significant bit of a

Prints a space between the bytes

(condition) ? (if true) : (if false)if (a&mask == 0) putchar(`0`);else putchar(`1`);

i % 8 == i & 7

Page 13: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

13

Pack 4 chars into an int#include <limits.h>

int pack( char a, char b, char c, char d ){

int p = a;

p = (p << CHAR_BIT) | b;p = (p << CHAR_BIT) | c;p = (p << CHAR_BIT) | d;return p;

}

p = 0 0 0 a p = 0 0 a 0 0 0 0 b 0 0 a b

p = 0 a b 0 0 0 0 c 0 a b c

p = a b c 0 0 0 0 d a b c d

Most significant Least significant

Page 14: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

14

Unpack a byte from an int

#include <limits.h>

char unpack( int p, int k )

{

unsigned mask = 0xFF;

int n = k * CHAR_BIT;

mask <<= n;

return ( ( p & mask ) >> n );

}

k = 0, 1, 2 or 3k = 0, 1, 2 or 3

n = 0, 8, 16 or 24n = 0, 8, 16 or 24

kth byte is onkth byte is on

Page 15: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

15

Operators Associativity

() [] . -> ++(postfix) --(postfix) left to right

+(unary) -(unary) ++(prefix) --(prefix) ! sizeof(type) &(address) *(dereference) ~

right to left

* / % left to right

+ - left to right

<< >> left to right

< <= > >= left to right

== != left to right

& left to right

^ left to right

| left to right

&& left to right

|| left to right

?: right to left

= += -= *= /= &= >>= etc right to left

,(comma operator) left to right

Operator precedence and associativity - final look

(a+b << 12*a >> b) is equivalent to (((a+b)<<(12*a))>>b)

Page 16: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

16

Software Project:Rehearsal

Page 17: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

17

Overview Rehearsal on selected topics in C

Stack (automatic) vs. Heap (dynamic) arrays vs. pointers structures Binary tree example (including function

pointers) Macros (preprocessor)

Sample exam questions Additional examples and topics

Page 18: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

18

Storage organization

Attempting to access an address outside the allocated partition (segment) of the program - a segmentation error (access violation) and core dump (only unix/linux).

The program stack grows for each function call

Page 19: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

19

Runtime Stack Activation records

(0) - inactive

(1) - inactive

(2) - active

Page 20: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

20

Arrays, Pointers and Strings

a b c d e \0p

Page 21: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

21

Arrays

void f1(){ ANY_TYPE a[10]; int b[] = {1,2}; char c1[] = {‘a’,’b’,’c’}; char c2[] = “abc”; printf(“%d %d %d %d \n”, sizeof(a), sizeof(b), sizeof(c1), sizeof(c2));}

Automatic allocation – on stack De-allocated automatically Overall size is given by sizeof()

Q: Assume sizeof(ANY_TYPE) = 5, sizeof(int)=4, sizeof(char)=1 What is the output of f1()?

A: 50 8 3 4

sizeof() is an operator that computes the size in bytes. It is evaluated at compile time.

constant expression (compilation time)

Page 22: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

22

Pointers and pointed values (1)

#include <stdio.h>

int main(){ int a = 10, b = 2; int *p1, *p2; p1 = &a; p2 = &b; printf("%d %d \n", *p1, *p2); printf("%p %p \n", p1, p2); return 0;}

0012FF7C

a: 100012FF78

b: 2

p1: p2:

0012FF7C 0012FF78

type = pointer.Prints a value of a pointer as an hexadecimal integer

Page 23: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

23

Pointers

pointer = address in memory Pointer definition: ANY_TYPE *p

ANY_TYPE: primitive types (char, int,...), structs, pointers,...

* operator – returns the pointed memory (the type of *p is ANY_TYPE) *p == p[0]

& operator = returns the address (pointer). p == &(*p)

Page 24: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

24

Pointers and pointed values (2)

#include <stdio.h>

int main(void){

int i = 42, j=17, *ptr;

ptr = &i;

*ptr = 1;

ptr = &j;

*ptr = 2;

printf ("The value of i is %d\n", i);

printf ("The value of j is %d\n", j);

return 0;

}

0012FF7C

i: 420012FF78

j: 17

ptr:

0012FF7C0012FF78

1 2

$%@#%

Page 25: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

25

Pointer initialization

void f1(){ char *p; *p = ‘a’; .....}

p is not initialized p contains “garbage”p may point to any memory address (legal / illegal)

void f2(int *a){ char *p; p = (char*)a; .....}

void f4(){ char *p; p = (char *)malloc(...); .....}

void f3(){ char c, *p; p = &c; .....}

X

assignment between any two pointers (using cast)

Dynamic allocation - on heap - no automatic de-allocation

Page 26: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

26

Pointers arithmetic

Q: If a pointer p contain the value x (address), what is the value of p+1?

A: depends on the type of p (ANY_TYPE *) => x+sizeof(ANY_TYPE)

p[n] == *(p+n)

Page 27: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

27

Pointers vs. Arrays Same arithmetic:

int a[10] *(a+2) == a[2] (no check of index boundaries, e.g. a[-1]) Pointers can point to elements in an array

Example: int a[10]; int *p = a; // p points to a[0] Pointers and arrays are not the same:

Pointer Array

Allocation malloc etc. – on heap. Any size.

On definition (automatic) - on stack. Size must be known at compilation

De-allocation Explicit command (free) Automatic

Constant inite.g. a = {1,2,3}

Disallowed Allowed - on declaration

Pointer / array Assignment (a = b)

Allowed Disallowed

sizeof Fixed pointer size (no relation to pointed memory)

Overall array size

Page 28: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

28

Example: printing the bytes of an integer

void print_bytes(int n){int i;char *pc = (char *)&n;

for (i=0; i < sizeof(int); i++, pc++){printf(“%d “,*pc);

}printf(“\n”);

}

char char char charint

Use sizeof(int) for portability.

Page 29: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

29

Multidimensional Arrays

int a[3][4];int i, j;

&a[i][j] = a[i]+j = (int *)a+i*4+j

int a[3][4];int i, j;

&a[i][j] = a[i]+j = (int *)a+i*4+j

C has only one-dimensional arrays:

(i,j) (i,j)<=>

Dimensions are known at compilation.

Page 30: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

30

Dynamic matrix allocation (1) Q: What if the matrix dimensions are

unknown at compilation? A: use pointers and dynamic allocation

The matrix can be ragged – i.e. rows can have different sizes.

int i;int **a = malloc(n*sizeof(int*));for (i=0; i <n; i++)

a[i] =malloc(m*sizeof(int));

int i;int **a = malloc(n*sizeof(int*));for (i=0; i <n; i++)

a[i] =malloc(m*sizeof(int));

a

Page 31: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

31

Dynamic matrix allocation (2)

Allocating an nm matrix using 2 allocationsint **allocate_matrix(int n, int m){ int i; int **a = malloc(n * sizeof(int *)); int *p = malloc(n * m * sizeof(int)); /* get the space all at once */ for (i = 0; i < n; i++, p+=m) a[i] = p; return a;}

a

p

Page 32: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

32

Strings

char *p = “abcde”;

a b c d e \0

char s[] = “abcde”;

a b c d e \0

s:

* Constant string “abcde” is kept somewhere1. Allocates a memory for pointer p2. Initializes p to the base address of “abcde”

Allocates and initializes 6 bytes for the arrays

p

Page 33: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

33

Allocating memory for strings

All strings have a terminating '\0' character. Add 1 to the count that strlen return when calling

malloc

char *somestring=“1234”, *copy;copy = (char *) malloc(strlen(somestring) + 1); /* +1 for \0 */ /* check malloc's return value */ strcpy(copy, somestring); int strlen(char s[]){

int i =0; while (s[i] != ‘\0’) ++i; return i;}

Page 34: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

34

Structs with Arraystypedef struct soldier {

int id; char name[64]; char *rank;} Soldier;

int main(){ Soldier s0, s1, s2 = {5705706, "Enzo Agada", "General"};

s0.id = 5705705; strcpy(s0.name, "John Doe"); /* incorrect: s0.name = "John Doe"; */ s0.rank = "Captain"; /* incorrect: strcpy(s0.rank, "Captain"); */ s1 = s0; printf("%d %s %s\n", s0.id, s0.name, s0.rank); printf("%d %s %s\n", s1.id, s1.name, s1.rank); printf("%d %s %s\n", s2.id, s2.name, s2.rank);}

5705705 John Doe Captain 5705705 John Doe Captain 5705706 Enzo Agada General

Page 35: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

35

Verifying allocated memory malloc may fail to allocate memory - returns a null

pointer upon failure. It is vital to check the returned pointer before

using it!! (even if the requested memory size is small)

int *ip = (int *) malloc(10000000 * sizeof(int)); if (ip == NULL) { printf("ERROR: out of memory\n"); return -1;

}

Page 36: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

36

(Heap) Memory de-allocation

Memory free == memory recycling Avoiding memory leaks:

Keep track of the allocated memory For every malloc there should be a corresponding free

The one who allocates is the one who frees free - a non-expensive function (allocating memory is more

expensive) Any allocated memory is automatically released upon exit by

the operating system. However, it is a coding standard to free the memory that you don’t need as soon as possible.

Page 37: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

37

Accessing de-allocated memory (1)

De-allocated memory should not be access! De-allocated heap-memory is immediately usable

for next memory allocation. After calling free(p); p still points at the same

memory Do not use the memory pointed to by p after

free(p);

while (ptr != NULL) { free(ptr); ptr = ptr->next;

}

Illegal reference to deallocated memory.Unknown results...

Page 38: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

38

Accessing de-allocated memory (2)

Local variables (stack memory) are de-allocatedautomatically on the return of their function

char *wrong_allocate(){ char s[] = "123"; return s;{char *valid_allocate(){ char* s1 = "456"; char* s = malloc(strlen(s1)+1); strcpy(s,s1); return s; }int main(int argc, char ** argv){ printf("%s %s \n",wrong_allocate(), valid_allocate());{

s points to a memory on stack, which is freed when the function returns

Page 39: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

39

Memory leaks

Memory leak = Non-reachable, non-freed allocated memory.

In java / c#: the garbage collector frees “memory leaks”

void f(){ int *p = malloc(10*sizeof(int));}

The memory pointed by p remains allocated. However there is no way to access it.

Page 40: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

40

realloc

accepts an old pointer and a new size Attempts to allocate memory in the original position.

If the size of the old block can be changed - the same pointer is returned.

otherwise, it Attempts to allocate new memory. If successful:

Copies old data to new memory Frees old memory block Returns new pointer

Otherwise, returns NULL (no free of ip)

int ip = malloc(10*sizeof(int);...... ip = realloc(ip, 200 * sizeof(int));

Page 41: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

41

Example: Sorted binary tree

typedef struct node {

int value;

struct node * right;

struct node * left;

} Node;

node_ptr->value;

(*node_ptr).value;

equivalent

value

value value

Left Right

struct node { int value; struct node * right; struct node * left; };typedef struct node Node;

equivalent

Page 42: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

42

Node allocation and initialization

Node *create_node(int value){ Node * tmp = (Node *) malloc(sizeof(Node)); if (!tmp) { fprintf(stderr, "ERROR: not enough memory!\n"); return NULL; } tmp->left = tmp->right = NULL; tmp->value = value; return tmp;}

Returns a new node1. Allocate memory for the new node.

3. Initialize node’s fields

2.Verify allocation

Page 43: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

43

Inserting new values to an existing treeNode * insert(Node * root, int value){

Node * parent, *child, *new_node; new_node = create_node(value); if (!new_node) return NULL; if (!root) return new_node; child = root; do{ parent = child; if (child->value > value) child = child->left; else child = child->right; } while (child); if (parent->value > value) parent->left = new_node; else parent->right = new_node; return root;}

2

3

5

7

9

4

parent

4

new_node root

Create the new node

Page 44: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

44

Traversing a binary tree (inorder)

void print_tree(Node *root)

{

if (!root) return;

print_tree(root->left);

printf("%d ", root->value);

print_tree(root->right);

}Printing the right subtree (recursive call)

Print the value of the current node.

Printing the right subtree (recursive call)

L R

P

Null Null Null Null

empty tree – do nothing

Page 45: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

45

Example: pointer to a function

Suppose we want to perform operations on the tree nodes – using our tree traversal function, E.g. printing the nodes

Pass a traversal function a pointer the desired function

void print_node(Node * node){ printf("%d ", node->value);}

prints the value of a node

traverse_tree(root, print_node);

Page 46: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

46

Example: pointer to a function (2) Define the type of the functions that the traversal

function can get

typedef void(Op)(Node *);

parameters typeReturn value

Data type name, which can be used to define variables / parameters

void traverse_tree(Node * root, Op * op){

if (!root) return;

traverse_tree(root->left, op);

op(root);

traverse_tree(root->right, op);

}

Invocation of the function (passed as argument op)

Page 47: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

47

Reminder: Things you can do with structures...

Define data types of structures Declare variables of structure type Assign entire structures Pass as arguments to functions Define functions which return structures Initialize a structure variable (comma-separated list of

values enclosed in braces {})

typedef struct node Node;

node1 = node2

Node node;

Node node = {0,NULL, NULL};

Page 48: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

48

Additional notetypedef struct node {

struct node * right; struct node * left; int value;

char *name;} Node;

void f(){Node *new_node;new_node = create_node(value);new_node->name = malloc(100);....free(new_node->name); free(new_node) ;

}

Every allocated field of a struct must be deallocated – before the struct itself is deallocated

Page 49: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

49

Macros (preprocessor)gcc -E: runs only the preprocessor and writes to standard output. E.g.: gcc -E a.c > aa.c

Page 50: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

50

Macros

Macros are not functions Even when written with all the parentheses

#define MAX(a,b) (((a)>(b)) ? (a) : (b))

int a[4];int biggest;

biggest = x[0];i = 1;while (i < 4) biggest = MAX(biggest, x[i++]);

#define MAX(a,b) (((a)>(b)) ? (a) : (b))

int a[4];int biggest;

biggest = x[0];i = 1;while (i < 4) biggest = MAX(biggest, x[i++]);

This code would have worked fine if MAX was a function

Page 51: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

51

Macros

Let’s expand the macro:

#define MAX(a,b) (((a)>(b)) ? (a) : (b))

int a[4];int biggest;

biggest = x[0];i = 1;while (i < 4) biggest = (biggest > x[i++]) ? biggest : x[i++];

#define MAX(a,b) (((a)>(b)) ? (a) : (b))

int a[4];int biggest;

biggest = x[0];i = 1;while (i < 4) biggest = (biggest > x[i++]) ? biggest : x[i++];

Page 52: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

52

Macros

Macros are not statements Lets try to write the assert macro

assert(x > y); /* should print info only if x <= y */

#define assert(e) if (!(e)) assert_error(__FILE__,__LINE__)

assert(x > y); /* should print info only if x <= y */

#define assert(e) if (!(e)) assert_error(__FILE__,__LINE__)

if ((x > 0) && (y > 0)) assert(x > y);else assert(y > x);

if ((x > 0) && (y > 0)) assert(x > y);else assert(y > x);

This macro is not safe:

Page 53: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

53

Macros

assert(x > y); /* should print info only if x <= y */

#define assert(e) { if (!e) assert_error(__FILE__,__LINE__) }

assert(x > y); /* should print info only if x <= y */

#define assert(e) { if (!e) assert_error(__FILE__,__LINE__) }

if ((x > 0) && (y > 0)) { if (!(x > y)) assert_error(__FILE__,__LINE__) };else { if (!(y > x)) assert_error(__FILE__,__LINE__) };

if ((x > 0) && (y > 0)) { if (!(x > y)) assert_error(__FILE__,__LINE__) };else { if (!(y > x)) assert_error(__FILE__,__LINE__) };

This will solve the dangling else problem but will cause a problemwith the semicolon:

We can try and add braces:

The correct solution is:

#define assert(e) ((void)((e) || assert_error(__FILE__,__LINE__)))

#define assert(e) ((void)((e) || assert_error(__FILE__,__LINE__)))

Page 54: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

54

Sample Exam Questions

Page 55: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

int main(void){ int a=1,b=2, c=3; what_happens(&a, &b, &c); printf("a=%d b=%d c=%d\n", a, b, c); return 0;

}void what_happens(int *a, int *b, int *c){

c = a; a = b; b = c;

printf("*a=%d *b=%d *c=%d\n", *a, *b, *c); return;}

a: b: c:

in what_happens:

0012FF7C

0012FF78 0012FF780012FF7C

0012FF78

0012FF8C0012FF7C

Printed 2 1 1

Question 1a: What is the output?

0012FF7C 0012FF78

b: 2

in main:

0012FF8C

c: 3a: 1

Printed 1 2 3

Page 56: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

int main(void){ int a=5,b = 7; int* c = do_balagan(&a, &b); printf("In main:\na=%d b=%d *c=%d\n", a, b, *c); return 0;}

int* do_balagan(int* a, int* b){ int* c; c = b; b = a; a = c; *a = *a + 5; *b = *a + *b; printf("In balagan:\n*a=%d *b=%d *c=%d\n", *a, *b, *c); return c;}

0012FF7C

a: 50012FF78

b: 7

in main:

12

a: b: c:

in do_balagan:

0012FF7C

0012FF78 0012FF780012FF7C

0012FF78

17

Question 1b: What is the output?

Page 57: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

57

real

image

1

2

c1:

real

image

3

5

c2:

1+2*2

abs2 = ((c2.image*c2.image)

+(c2.real*c2.real)) =34

abs1 = ((c1.image*c1.image)+(c1.real*c1.real)) =5

Reminder: the preprocessor performs textual substitutions before compilationQuestion 2: What is the output?

Page 58: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

58

1st invocation:initial: a=2, count =0;final: count = 1*2 = 2;

2nd invocation:initial: a=2, count = 2;final: count =3*2 = 6;

Question 3: What is the output?

Page 59: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

59

Pointer to the next entry

If it is the end of a word increment the count

If there is no space after the last word

Question 4: -Is the code valid? If no - why?-If yes - What does it do?(isspace returns 1 iff the given character is a white space)C

What happens if strlen(s)=0? (i.e. s==`\0`)

Page 60: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

60

Access to a freed pointer!!!

Question 5: -Is the code valid? If no - why?-If yes - What does it do?

Page 61: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

61

Exam Grading (previous semester)

Page 62: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

62

Good Luck in the exam!

(30.03.2008)

Page 63: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

63

Additional Examples and topics

Page 64: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

64

getline() function - Description

Implement a function int getline(char line[], int max); Description: Reads from standard input into a given line array

till new-line, end-of-file, the given array is filled.If newline was reached – it is not copied into the given array.

line – the given array to be filled (output parameter) max – the size of the given array (i.e. maximal characters to

read) Return value = line length

0 – for empty line EOF – for end-of-file (and nothing was read)

Page 65: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

65

getline() function - Usage

#include <stdio.h>#include <stdlib.h>

#define MAX_LINE 80

int main(){ char line[MAX_LINE]; getline(line, MAX_LINE); printf("%s\n", line); return 0;}

Define a fixed size array and pass it to the getline function

The getline function reads a line and we print it

Page 66: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

66

int getline(char line[], int max){ int nch = 0, c; max = max - 1; while ( ( (c = getchar()) != EOF) && (c != '\n') && (nch<max) ){

line[nch] = c; nch++; } if (c == EOF && (nch == 0)) return EOF; line[nch] = '\0'; return nch;}

getline() function – Implementation

Reserve a place for ‘\0’

May happen when stdout is redirected to a text file.

Page 67: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

67

getline() –Usage (dynamic allocation)int main(){

char * line; int linelen; printf("Specify the sentence length:\n"); scanf("%d", &linelen); if (linelen <= 0) { printf("ERROR: line length must be positive!\n"); return 1; } line = (char *) malloc(linelen); if (line == NULL) { printf("ERROR: out of memory!\n"); return 1; } flushall(); printf("\nPlease type your sentence:\n"); getline(line, linelen); printf("\nYou typed:\n%s\n",line);

return 0;}

You could have written:line = (char *) calloc (linelen, sizeof(char));

malloc() – allocates the required amount of bytes.

calloc() – allocates and initializes to zero bytes.

Page 68: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

68

Searching for Substrings – Impl.

/* Finds out whether a given array appears as a sub array in another */

int * find_sub_array(int * array, int size, int * sub_array, int sub_size) {

int i, j; for (i = 0; i++ <= size - sub_size; ++array) { for (j = 0; j < sub_size; ++j){ if ( *(array + j) != sub_array[j]) break;

} if (j == sub_size) return array; } return NULL;}

Counter “i” is used to validate that we are still within the array boundaries. Note that i is incremented only after the condition i<=size-sub_size is evaluated

Page 69: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

69

Searching for Substrings - Usage

#include <stdio.h>int main(){ int array1[] = {1, 45, 67, 1001, -19, 67, 89, 1004, -867, 34, 3, -1900,

10029, 34, 3, -1900}; int array2[] = {34, 3, -1900}; int * position; int offset = -1; position = find_sub_array(array1, sizeof (array1) / sizeof (int), array2, sizeof (array2) / sizeof(int)); if (position != NULL)

offset = position - array1 + 1; printf("array2 appears in array1 starting from place : %d.\n", offset); return 0;}

Page 70: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

70

Dangling else

The meaning was two cases: x = 0 and x != 0 However else is associated with the closest unmatched if.

if (x == 0) if (y == 0) error();else { z = x + y; f(z);}

if (x == 0) if (y == 0) error();else { z = x + y; f(z);}

if (x == 0) if (y == 0) error(); else { z = x + y; f(z); }

if (x == 0) if (y == 0) error(); else { z = x + y; f(z); }

Page 71: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

71

Dangling else

Always use curly braces ‘{}’ with if-else statements

if (x == 0) { if (y == 0) error();} else { z = x + y; f(z);}

if (x == 0) { if (y == 0) error();} else { z = x + y; f(z);}

Page 72: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

72

Using CORE dumps

On Unix (including Linux) a process is usually killed if it access memory incorrectly This usually happens due to pointers going mad

and bad Before the process is killed important

information on why it was killed is saved in a core file This information can be used to pinpoint what

went wrong

Page 73: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

73

Using CORE dumps

#include <stdlib.h>

int main() { int* i = NULL;

*i = 5; /* This will cause a core dump in Unix/Linux */

return 0;}

#include <stdlib.h>

int main() { int* i = NULL;

*i = 5; /* This will cause a core dump in Unix/Linux */

return 0;}

Lets look at this faulty program (test.c) that tries to access a NULL pointer

Running this program yields the expected error message:Segmentation fault (core dumped)

Page 74: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

74

Using CORE dumps

To use the core dump the program must be compiled with debug information.

This is done by adding –g flag to the gcc compilation line For example: gcc –g test.c –o myTest

(Make sure that your core file was generated while running the program compiled with debug information before continuing to the next step - you may need to run the program again after adding flag)

gdb <program executable> coreorddd <program executable> core

gdb <program executable> coreorddd <program executable> core Graphical

Non graphical

Page 75: 1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.

75

Using CORE dumps

nova~> gdb myTest coreGNU gdb Red Hat Linux (5.2-2)Copyright 2002 Free Software Foundation, Inc.Core was generated by `./myTest'.Program terminated with signal 11, Segmentation fault.Reading symbols from /lib/i686/libc.so.6...done.Loaded symbols for /lib/i686/libc.so.6Reading symbols from /lib/ld-linux.so.2...done.Loaded symbols for /lib/ld-linux.so.2#0 0x080483d0 in main () at test.c:66 *i = 5;(gdb) where#0 0x080483d0 in main () at test.c:6#1 0x42017589 in __libc_start_main () from /lib/i686/libc.so.6(gdb)

nova~> gdb myTest coreGNU gdb Red Hat Linux (5.2-2)Copyright 2002 Free Software Foundation, Inc.Core was generated by `./myTest'.Program terminated with signal 11, Segmentation fault.Reading symbols from /lib/i686/libc.so.6...done.Loaded symbols for /lib/i686/libc.so.6Reading symbols from /lib/ld-linux.so.2...done.Loaded symbols for /lib/ld-linux.so.2#0 0x080483d0 in main () at test.c:66 *i = 5;(gdb) where#0 0x080483d0 in main () at test.c:6#1 0x42017589 in __libc_start_main () from /lib/i686/libc.so.6(gdb)

Your command line

The gdb analysis of the core


Recommended