240 likes | 252 Views
Learn about using bits for logical operations, including masking, shifting, parity, and character representation. Understand the software life cycle, basic stages, and verification via loop invariants. Explore the costs associated with software development.
E N D
CompSci 105 SS 2005 Principles of Computer Science Lecture 3 Lecturer: Santokh Singh
Using Bits as Bits Logical Operations Masking and Shifting Parity Representation of Characters Software Life Cycle Basic Stages Specification and Design Verification via Loop Invariants Costs Associated with Software
Octal and Hex in Java int i = 12; System.out.println(i); int i = 012; System.out.println(i); int i = 0x12; System.out.println(i);
Using Bits as Bits Logical Operations Masking and Shifting Parity Representation of Characters Software Life Cycle Basic Stages Specification and Design Verification via Loop Invariants Costs Associated with Software
//First things first – announcements: if (you_enrolled_in _the_105course && you_did_not_pass_COMPSCI101 && you_do_not_have_a_concession_form) { you_MAY_have_been_withdrawn_from_the_course = true; System.out println(“Please check out your fate, urgently.”); }
Logical Operations • NOT x • x AND y • x OR y • x EOR y (x XOR y)
Bitwise Operations • NOT x • x AND y • x OR y • x EOR y (x XOR y)
1 0 1 1 & 1 1 1 0
Exercise • What is the output? int i = 14; System.out.println( i & 1 ); System.out.println( i & 5 );
0101 1011 1100 1010 0001 1001 1101 0111 0110 0100 0011 0010 1000 10 Q K A 9 8 6 3 7 2 5 4 J Representing a Card 00 ♠ 01 ♣ 10 ♥ 11 ♦
A card in six bits 10 0111
Extracting the Value 10 0111 & 00 1111
Extracting the Suit 10 0111 & 11 0000
Extracting the Suit 10 0111 & 11 0000 What is the suit mask in Hex?
In Java…. public final int valueMask = 0x0f; public final int suitMask = 0x30; public final int suitSpades = 0x00; public final int suitClubs = 0x10; public final int suitHearts = 0x20; public final int suitDiamonds = 0x30; int card = 7 | suitHearts; if ( (card & suitMask) == suitHearts ) System.out.println(“Hearts”);
Extracting the Suit 10 0111 & 11 0000 10 0000
Shifting operations 01100010 << 2 01100010 >> 2
In Java…. public final int suitSpades = 0; public final int suitClubs = 1; public final int suitHearts = 2; public final int suitDiamonds = 3; int card = 7 | ( suitHearts << 4 );
Using Bits as Bits Logical Operations Masking and Shifting Parity Representation of Characters Software Life Cycle Basic Stages Specification and Design Verification via Loop Invariants Costs Associated with Software
Representation of Characters • How many bits do we need?
Unicode • 16-bit, Encodes International Alphabets • Low-order 7-bits are ASCII • System.out.println( (int)’E’ ); • www.unicode.org (Ladies & Gentlemen: Assignment 1 given was given out last week).