21
04 ררררררCopyright Meir Kalech 1 C programming Language Chapter 9 : Bitwise Structures

ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

  • View
    222

  • Download
    2

Embed Size (px)

Citation preview

Page 1: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 1

C programming Language

Chapter 9 :Bitwise Structures

Page 2: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 2

What is Bitwise Structure? The smallest type is of 8 bits (char). Sometimes we need only a single bit. For instance, storing the status of the lights in 8

rooms: We need to define an array of at least 8 chars.

If the light of room 3 is turned on the value of the third char is 1, otherwise 0.

Total array of 64 bits.

EXPENSIVE in place and time!!!

Page 3: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 3

What is Bitwise Structure? It is better to define only 8 bits since a bit can also

store the values 0 or 1. But the problem is that there is no C type which is

1 bit long (char is the longer with 1 byte). Solution: define a char (8 bits) but refer to each bit

separately. Bitwise operators, introduced by the C language,

provide one of its more powerful tools for using and manipulating memory. They give the language the real power of a “low-level language”.

Page 4: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 4

What is Bitwise Structure? Accessing bits directly is fast and efficient,

especially if you are writing a real-time application.

A single bit cannot be accessed directly, since it has no address of its own.

The language introduces the bitwise operators, which help in manipulating a single bit of a byte.

bitwise operators may be used on integral types only (unsigned types are preferable).

Page 5: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 5

Bitwise Operators

&bitwise AND

|bitwise OR

^bitwise XOR

~1’s compliment

<<Shift left

>>Shift right

All these operators can be suffixed with =For instance a &= b; is the same as a = a & b;

Page 6: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 6

Bitwise Operators – truth table

aba&ba|ba^b~a

000001

010111

100110

111100

Page 7: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 7

Bitwise Operators - Examples

11010011&

10001100------------

10000000

11010011|

10001100------------

11011111

11010011^

10001100------------

01011111

~11010011------------

00101100

11010011>>3------------

00011010

11010011<<3------------

10011000

Page 8: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 8

Setting Bits How can we set a bit on or off?

Manipulations on bits are enabled by mask and bitwise operators.

Bitwise OR of anything with 1 results in 1.

Bitwise AND of anything with 0 results in 0.

Page 9: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 9

Setting Bits For instance, how can we turn on the

light in room #3?

char lights = 0x0;char mask = 0x1;mask <<= 2;lights |= mask;

lights: 00000000

mask: 00000001

mask: 00000100

lights: 00000100

Page 10: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 10

Setting Bits For instance, how can we turn off

the light in room #3?

char lights = 0x27;char mask = 0xfb;lights &= mask;

lights: 00100111

mask: 11111011

lights: 00100011

Page 11: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 11

Getting Bits

How can we know if a bit is on or off?

Manipulations on bits are enabled by mask and bitwise operators.

Bitwise AND of anything with 1 results in the same value.

Page 12: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 12

Getting Bits For instance, how can we check if

the light in room #3 is turned on or off?

char lights = 0x27;char mask = 0x1;mask <<= 2;if(lights & mask) puts(“turned on”);else puts(“turned off”);

lights: 00100111

mask: 00000001

mask: 00000100

lights & mask: 00000100

Page 13: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 13

Bitwise - Example

Suppose we have 8 rooms: Light is on in the rooms requested by the

user.

Print which rooms are lighted.

void main(){

unsigned char lights = 0;set_lights(&lights);print_lights(lights);

}

Page 14: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 14

Bitwise - Example

void set_lights(unsigned char *lights){

int j, answer;unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1){

answer=0;printf(“lights in room #%d?”, j+1);scanf(“%d”, &answer);if(answer)

*lights |= mask;}

}

00000000

lights

00000001

mask

answer: 1 (yes)

00000001

*lights |= mask

Page 15: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 15

Bitwise - Example

00000001

lights

00000010

mask

answer: 0 (no)

00000001

void set_lights(unsigned char *lights){

int j, answer;unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1){

answer=0;printf(“lights in room #%d?”, j+1);scanf(“%d”, &answer);if(answer)

*lights |= mask;}

}

*lights |= mask

Page 16: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 16

Bitwise - Example

00000001

lights

00000100

mask

answer: 1 (yes)

00000101

void set_lights(unsigned char *lights){

int j, answer;unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1){

answer=0;printf(“lights in room #%d?”, j+1);scanf(“%d”, &answer);if(answer)

*lights |= mask;}

}

*lights |= mask

Page 17: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 17

Bitwise - Example

00000101

lights

00001000

mask

answer: 1 (yes)

00001101

void set_lights(unsigned char *lights){

int j, answer;unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1){

answer=0;printf(“lights in room #%d?”, j+1);scanf(“%d”, &answer);if(answer)

*lights |= mask;}

}

*lights |= mask

Page 18: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 18

Bitwise - Example

00001101

lights

00000001

mask

00000001

lights & mask

void print_lights(unsigned char lights){ int j; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) { if(lights & mask) printf(“room #%d is lighted”,j+1); else printf(“room #%d is NOT lighted”,j+1); }} Output:

room #1 is lighted

Page 19: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 19

Bitwise - Example

00001101

lights

00000010

mask

00000000

lights & mask

void print_lights(unsigned char lights){ int j; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) { if(lights & mask) printf(“room #%d is lighted”,j+1); else printf(“room #%d is NOT lighted”,j+1); }} Output:

room #2 is NOT lighted

Page 20: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 20

Bitwise - Example

00001101

lights

00000100

mask

00000100

lights & mask

void print_lights(unsigned char lights){ int j; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) { if(lights & mask) printf(“room #%d is lighted”, j+1); else printf(“room #%d is NOT lighted”, j+1); }} Output:

room #3 is lighted

Page 21: ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 9: Bitwise Structures

ספטמבר 04 Copyright Meir Kalech 21

Bitwise - Example

00001101

lights

00001000

mask

00001000

lights & mask

void print_lights(unsigned char lights){ int j; unsigned char mask; for(j=0,mask=1; j<8; j++,mask<<=1) { if(lights & mask) printf(“room #%d is lit”, j+1); else printf(“room #%d is NOT lit”, j+1); }} Output:

room #4 is lighted