Docsity

Binary and Bit Operations - Data Organization - Slides | CS 241L, Study notes of Computer Science

Toggle sidebar
bg1
1
2/19/2009
Binary and Bit Operators
CS 241
Data Organization using C
Instructor: Joel Castellanos
Web: http://cs.unm.edu/~joel/
Office: Farris Engineering
Center (FEC) room 321
Lab Instructor: Dongye Meng
2
Note Taker Needed โ€“ Earn $$$
๎š„Accessibility Resource Center and/or Deaf and
Hard of Hearing Services are looking for a
student in this class to volunteer to provide
notes.
๎š„The student will be paid a stipend to take notes.
๎š„The interested student should come by
Accessibility Resource Center office at 2021
Mesa Vista Hall to complete the required hiring
paperwork.
๎š„Please bring your Lobo Card with you.
pf3
pf4
pf5

Partial preview of the text

Download Binary and Bit Operations - Data Organization - Slides | CS 241L and more Study notes Computer Science in PDF only on Docsity!

2/19/

Binary and Bit Operators

CS 241

Data Organization using C

Instructor: Joel Castellanos e-mail : [email protected] Web: http://cs.unm.edu/~joel/ Office: Farris Engineering Center (FEC) room 321 Lab Instructor: Dongye Meng e-mail: [email protected]

2

Note Taker Needed โ€“ Earn $$$

ย„ Accessibility Resource Center and/or Deaf and

Hard of Hearing Services are looking for a

student in this class to volunteer to provide

notes.

ย„ The student will be paid a stipend to take notes.

ย„ The interested student should come by

Accessibility Resource Center office at 2021

Mesa Vista Hall to complete the required hiring

paperwork.

ย„ Please bring your Lobo Card with you.

4 SanDisk 8-Gigabit Flash Memory Chip How many bytes of memory does this chip

  • Powers of
  • ย„ 2 1 =
  • ย„ 2 2 = 2ร—2=
  • ย„ 2 3 = 2ร—2ร—2=
  • ย„ 2 4 = 2ร—2ร—2ร—2=
  • ย„ 2 5 =
  • ย„ 2 6 =
  • ย„ 2 7 =
  • ย„ 2 8 =
  • ย„ 2 9 =
  • ย„ 2 10 = - ย„ Binary with 3 bits has - ย„ 0: 000 = 0 2 + 0 1 + = 8 Permutations : - ย„ 1: 001 = 0 2 + 0 1 + - ย„ 2: 010 = 0 2 + 2 1 + - ย„ 3: 011 = 0 2 + 2 1 + - ย„ 4: 100 = 2 2 + 0 1 + - ย„ 5: 101 = 2 2 + 0 1 + - ย„ 6: 110 = 2 2 + 2 1 + - ย„ 7: 111 = 2 2 + 2 1 +

7

Bitwise Operators

ย„ & bitwise AND 1010 & 0011 = 0010

ย„ | bitwise inclusive OR 1010 | 0011 = 1011

ย„ ^ bitwise exclusive OR 1010 ^ 0011 = 1001

ย„ << left shift 00000100 << 3 = 00100000

ย„ >> right shift 00000100 >> 2 = 00000001

8

Shift Operator Example

  1. int main()
  2. { int i;
  3. for (i=0; i<8; i++)
  4. { unsigned char n = 1 << i;
  5. printf("n=%d\n", n);
  6. }
  7. } (^) n= n= n= n= n= n= n= n=

Output:

9

<< and & Example: #

  1. int main()
  2. { unsigned char a=17;
  3. int i;
  4. for (i=7; i>=0; i--)
  5. { unsigned char n = 1 << i;
  6. if (a & n) printf("1");
  7. else printf("0");
  8. }
  9. printf("\n");

Output:^00010001

10

<< and & Example: #

  1. int main()
  2. { unsigned char a=22;
  3. int i;
  4. for (i=7; i>=0; i--)
  5. { unsigned char n = 1 << i;
  6. if (a & n) printf("1");
  7. else printf("0");
  8. }
  9. printf("\n");

Output:^00010110

13

Two's Complement

Two's Complement:

11111000 โˆ’ 8

11111001 โˆ’ 7

11111010 โˆ’ 6

11111011 โˆ’ 5

11111100 โˆ’ 4

11111101 โˆ’ 3

11111110 โˆ’ 2

11111111 โˆ’ 1

Two's Comp Decimal

00000000 0

00000001 1

00000010 2

00000011 3

00000100 4

00000101 5

00000110 6

00000111 7

Two's Comp Decimal

-5:

Add 1:

Flip Bits:

5: 1 1 1 1 0 1 0 0 0 0 0 0 0 1 1 1 1 1 0 1 1

0 0 0 0 1 0 1