Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
Recall boolean logical operators in Java… • boolean logical operators: &, |, ^ • not: ! Also conditional operators: &&, || • Difference between & and &&. • Differences between | and ||. • How can we demonstrate this?
Bitwise integer operators • HLL such as Java and C/C++ support many bitwise integer operators. • Java: • bitwise: &, ^, |, -, ~ • shift: <<, >>, >>> (Note: C/C++ supports all of the above except >>>.)
Bitwise integer operators • Note: In Java and C/C++ as in Assembler, the bitwise operators can be applied to any of the integer data types (char, short, int, and long). • Note: All of the following examples employ 8-bit (byte) or 16-bit (short) integers but the ideas can and are extended to other integer data sizes. • Note: All of the following examples represent numbers in hex and binary for convenience. These rules apply to decimal numbers as well.
~ (1’s complement) • Given 0x3a, what is ~0x3a? • Given 0x0022, what is ~0x0022?
- (2’s complement) • Given 0x3a, what is -0x3a? • Given 0x0022, what is -0x0022?
Shift operators • Left shift: A << B • Signed right shift: A >> B (sign extension) • Unsigned right shift: A >>> B (zero extension) A = value to be shifted B = shift distance
Examples int b1 = 0x65; int b2 = 0xaf; int x = b1 & b2; int y = b1 ^ b2; int z = b1 | b2;
Problem 1 • How can we determine if an int is odd or even?
Problem 2 • Using bitwise operators, write a program in Java that converts an int from its current endian-ness to the other.
Problem 3 • Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0; // What does this indicate? // How do I indicate that student #7 took the quiz?
Problem 3 • Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0; // indicate that no one took the quiz quiz1 |= 1 << 7; // indicate that student #7 took the quiz // How do I indicate that student #4 took the quiz?
Problem 3 • Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0; // indicate that no one took the quiz quiz1 |= 1 << 7; // indicate that student #7 took the quiz quiz1 |= 1 << 4; // indicate that student #4 took the quiz // Oops! Student #7 didn’t take the quiz. Turn it off.
Problem 3 • Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0; // indicate that no one took the quiz quiz1 |= 1 << 7; // indicate that student #7 took the quiz quiz1 |= 1 << 4; // indicate that student #4 took the quiz quiz1 &= ~(1 << 7); // Oops! Student #7 didn’t take the quiz. Turn it off.
Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0; // indicate that no one took the quiz quiz1 |= 1 << 7; // indicate that student #7 took the quiz quiz1 |= 1 << 4; // indicate that student #4 took the quiz quiz1 &= ~(1 << 7); // Oops! Student #7 didn’t take the quiz. Turn it off. int quiz2 = 0; // let’s give another quiz! … // indicate students that took quiz 2 // How can we determine if student #4 took both?
Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0; // indicate that no one took the quiz quiz1 |= 1 << 7; // indicate that student #7 took the quiz quiz1 |= 1 << 4; // indicate that student #4 took the quiz quiz1 &= ~(1 << 7); // Oops! Student #7 didn’t take the quiz. Turn it off. int quiz2 = 0; // let’s give another quiz! … // indicate students that took quiz 2 // determine if student #4 took both final int s7 = 1<<7; if ( (quiz1&s7) != 0 && (quiz2&s7) != 0 ) { … //student 7 took both }
Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0; // indicate that no one took the quiz quiz1 |= 1 << 7; // indicate that student #7 took the quiz quiz1 |= 1 << 4; // indicate that student #4 took the quiz quiz1 &= ~(1 << 7); // Oops! Student #7 didn’t take the quiz. Turn it off. int quiz2 = 0; // let’s give another quiz! … // indicate students that took quiz 2 // How can we print out all students that took either quiz 1 or quiz 2 // (but not both)?
Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0; // indicate that no one took the quiz quiz1 |= 1 << 7; // indicate that student #7 took the quiz quiz1 |= 1 << 4; // indicate that student #4 took the quiz quiz1 &= ~(1 << 7); // Oops! Student #7 didn’t take the quiz. Turn it off. int quiz2 = 0; // let’s give another quiz! … // indicate students that took quiz 2 // determine whether a student took either quiz 1 or quiz 2 (but not both) int oneOrTheOther = quiz1 ^ quiz2; // How do we print the students out?
Problem 3 Say class sizes are limited to 32 students. Therefore, I can use the bits in an int to indicate whether or not students took a particular quiz: int quiz1 = 0; // indicate that no one took the quiz quiz1 |= 1 << 7; // indicate that student #7 took the quiz quiz1 |= 1 << 4; // indicate that student #4 took the quiz quiz1 &= ~(1 << 7); // Oops! Student #7 didn’t take the quiz. Turn it off. int quiz2 = 0; // let’s give another quiz! … // indicate students that took quiz 2 // determine whether a student took either quiz 1 or quiz 2 (but not both) int oneOrTheOther = quiz1 ^ quiz2; // print out the students if ( (oneOrTheOther & 1) != 0 ) System.out.println( “one” ); oneOrTheOther >>>= oneOrTheOther; if ( (oneOrTheOther & 1) != 0 ) System.out.println( “two” ); …
AND • Operation: DEST ← DEST AND SRC; • Flags Affected: • The OF and CF flags are cleared. • the SF, ZF, and PF flags are set according to the result. • The state of the AF flag is undefined.
NEG (2’s) • Operation: IF DEST = 0 THEN CF ← 0 ELSE CF ← 1; FI; DEST ← – (DEST) • Flags Affected: • The CF flag set to 0 if the source operand is 0; otherwise it is set to 1. • The OF, SF, ZF, AF, and PF flags are set according to the result.
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.