java and and or java xor and not
play

Java AND and OR Java XOR and NOT AND operator (&) - PDF document

Java Bitwise Operators Java has six bitwise operators: Miscellaneous Java Symbol Operator & Bitwise AND TOPICS | Bitwise OR Bit Operators ^ Bitwise XOR


  1. Java ¡Bitwise ¡Operators ¡ • Java has six bitwise operators: Miscellaneous Java Symbol ¡ Operator ¡ & ¡ Bitwise ¡AND ¡ TOPICS | ¡ Bitwise ¡OR ¡ • Bit Operators ^ ¡ Bitwise ¡XOR ¡ • Character and String Classes ~ ¡ Bitwise ¡NOT ¡ • Integer and Double Classes • Arrays Class << ¡ LEFT ¡SHIFT ¡ >> ¡ RIGHT ¡SHIFT ¡ CS 160, Spring Semester 2013 2 Java ¡AND ¡and ¡OR ¡ Java ¡XOR ¡and ¡NOT ¡ AND operator (&) OR operator (|) XOR operator (^) NOT operator (~) A ¡ B ¡ A ¡& ¡B ¡ A ¡ B ¡ A ¡| ¡B ¡ A ¡ B ¡ A ¡^ ¡B ¡ A ¡ ~A ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 0 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 0 ¡ 0 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ 0 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 1 ¡ 0 ¡ CS 160, Spring Semester 2013 3 CS 160, Spring Semester 2013 4 1

  2. Binary ¡to ¡Decimal ¡ Binary ¡to ¡Decimal ¡ • 0-­‑9 ¡are ¡used ¡for ¡decimal ¡numbers ¡(base-­‑10): ¡ Decimal ¡ Binary ¡ Decimal ¡ Binary ¡ – 149 ¡= ¡1*10 2 ¡+ ¡4*10 1 ¡+ ¡9*10 0 ¡ 0 ¡ 0000b ¡ 8 ¡ 1000b ¡ • 0-­‑1 ¡are ¡used ¡for ¡binary ¡numbers ¡(base-­‑2): ¡ 1 ¡ 0001b ¡ 9 ¡ 1001b ¡ – 1010b ¡= ¡1*2 3 ¡+ ¡0*2 2 ¡+ ¡1*2 1 ¡+ ¡*2 0 ¡ = ¡8 ¡+ ¡2 ¡= ¡10 ¡ 2 ¡ 0010b ¡ 10 ¡ 1010b ¡ • Example: ¡ 3 ¡ 0011b ¡ 11 ¡ 1011b ¡ – 10111b ¡in ¡decimal? ¡ 4 ¡ 0100b ¡ 12 ¡ 1100b ¡ – 1*2 4 ¡+ ¡0*2 3 ¡+ ¡1*2 2 ¡+ ¡1*2 1 ¡+ ¡1*2 1 ¡= ¡16 ¡+ ¡4 ¡+ ¡2 ¡+ ¡1 ¡= ¡23 ¡ 5 ¡ 0101b ¡ 13 ¡ 1101b ¡ – What ¡is ¡14 ¡in ¡binary? ¡ 6 ¡ 0110b ¡ 14 ¡ 1110b ¡ – 8 ¡+ ¡4 ¡+ ¡2 ¡= ¡1*2 3 ¡+ ¡1*2 2 ¡+ ¡1*2 1 ¡+ ¡0*2 0 ¡= ¡1110b ¡ 7 ¡ 0111b ¡ 15 ¡ 1111b ¡ CS 160, Spring Semester 2013 5 CS 160, Spring Semester 2013 6 Bitwise ¡Operator ¡Examples ¡ Masking ¡Opera^ons ¡ • 4-­‑bit ¡numbers: ¡ • Clearing ¡bits: ¡ – 6 ¡& ¡5 ¡= ¡0110b ¡& ¡0101b ¡= ¡0100b ¡= ¡4 ¡ – x ¡= ¡00101001b ¡= ¡41 ¡ – 6 ¡| ¡5 ¡= ¡ ¡0110b ¡| ¡0101b ¡= ¡0111b ¡= ¡7 ¡ – want ¡to ¡clear ¡top ¡4-­‑bits ¡ – 6 ¡^ ¡5 ¡= ¡0110b ¡^ ¡0101b ¡= ¡0011b ¡= ¡3 ¡ – x ¡= ¡x ¡& ¡00001111b ¡= ¡x ¡& ¡15 ¡= ¡00001001b ¡= ¡9 ¡ – ~6 ¡= ¡~0110b ¡= ¡1001b ¡= ¡9 ¡ • Seang ¡bits: ¡ • 8-­‑bit ¡numbers: ¡ – x ¡= ¡00101001b ¡= ¡41 ¡ – 6 ¡<< ¡3 ¡= ¡00000110b ¡<< ¡3 ¡= ¡00110000b ¡= ¡48 ¡(6 ¡* ¡8) ¡ – want ¡to ¡set ¡bobom ¡4-­‑bits ¡ – 48 ¡>> ¡4 ¡= ¡00110000b ¡>> ¡4 ¡= ¡00000011b ¡= ¡3 ¡(48 ¡/ ¡16) ¡ – x ¡= ¡x ¡| ¡00001111b ¡= ¡x ¡| ¡15 ¡= ¡00101111b ¡= ¡47 ¡ CS 160, Spring Semester 2013 7 CS 160, Spring Semester 2013 8 2

  3. Character ¡Class ¡ String ¡Class ¡ • Methods ¡that ¡detect ¡types ¡of ¡characters: ¡ • Methods ¡that ¡manipulate ¡strings: ¡ – Character.isUpperCase(char ¡c); ¡ – String.toUpperCase(); ¡ – Character.isLowerCase(char ¡c); ¡ – String.toLowerCase(); ¡ – Character.isDigit(char ¡c); ¡ – String.substring(int ¡beginIndex, ¡int ¡endIndex)l ¡ – Character.isLeber(char ¡c); ¡ • Example ¡using ¡String ¡s ¡= ¡“HelloThere”; ¡ – Character.isSpace(char ¡c); ¡ – s.toUpperCase() ¡returns ¡“HELLOTHERE” ¡ • Example: ¡ – s.toLowerCase() ¡returns ¡“hellothere” ¡ – Character.isLeber(‘8’) ¡== ¡false ¡ – s.substring(2,7) ¡returns ¡“lloTh” ¡ – Character.isLowerCase(‘a’) ¡== ¡true ¡ CS 160, Spring Semester 2013 9 CS 160, Spring Semester 2013 10 Integer ¡and ¡Double ¡Classes ¡ Arrays ¡Class ¡ • Methods ¡that ¡parse ¡strings ¡to ¡return ¡numbers: ¡ • Methods ¡to ¡manipulate ¡arrays: ¡ – Integer.parseInt(String ¡s); ¡ – Arrays.toString(int ¡array[]); ¡ – Double.parseDouble(String ¡s); ¡ – Arrays.sort(int ¡array[]); ¡ – Arrays.equals(int ¡a1[], ¡int ¡a2[]); ¡ • Example: ¡ • Example ¡using ¡int ¡array[] ¡= ¡{4, ¡3, ¡5, ¡2, ¡1}; ¡ – Integer.parseInt(“154”) ¡returns ¡154 ¡ – Double.parseDouble(“12.5”) ¡returns ¡12.5 ¡ – Arrays.toString(array) ¡returns ¡“[4, ¡3, ¡5, ¡2, ¡1]” ¡ – Integer.parseInt(“Hello”) ¡gets ¡an ¡excep^on ¡ – Arrays.sort(array) ¡sorts ¡array ¡to ¡{1, ¡2, ¡3, ¡4, ¡5} ¡ ¡ – Double.parseDouble(“There”) ¡gets ¡an ¡excep^on ¡ – Arrays.equals(array, ¡array} ¡= ¡true ¡ CS 160, Spring Semester 2013 11 CS 160, Spring Semester 2013 12 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend