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 ¡ • More Java >> ¡ RIGHT ¡SHIFT ¡ CS 160, Fall Semester 2015 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, Fall Semester 2015 3 CS 160, Fall Semester 2015 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 ¡ 0000 ¡ 8 ¡ 1000 ¡ • 0-­‑1 ¡are ¡used ¡for ¡binary ¡numbers ¡(base-­‑2): ¡ 1 ¡ 0001 ¡ 9 ¡ 1001 ¡ – 0b1010 ¡= ¡1*2 3 ¡+ ¡0*2 2 ¡+ ¡1*2 1 ¡+ ¡*2 0 ¡ = ¡8 ¡+ ¡2 ¡= ¡10 ¡ 2 ¡ 0010 ¡ 10 ¡ 1010 ¡ • Example: ¡ 3 ¡ 0011 ¡ 11 ¡ 1011 ¡ – 10111b ¡in ¡decimal? ¡ 4 ¡ 0100 ¡ 12 ¡ 1100 ¡ – 1*2 4 ¡+ ¡0*2 3 ¡+ ¡1*2 2 ¡+ ¡1*2 1 ¡+ ¡1*2 1 ¡= ¡16 ¡+ ¡4 ¡+ ¡2 ¡+ ¡1 ¡= ¡23 ¡ 5 ¡ 0101 ¡ 13 ¡ 1101 ¡ – What ¡is ¡14 ¡in ¡binary? ¡ 6 ¡ 0110 ¡ 14 ¡ 1110 ¡ – 8 ¡+ ¡4 ¡+ ¡2 ¡= ¡1*2 3 ¡+ ¡1*2 2 ¡+ ¡1*2 1 ¡+ ¡0*2 0 ¡= ¡0b1110 ¡ 7 ¡ 0111 ¡ 15 ¡ 1111 ¡ CS 160, Fall Semester 2015 5 CS 160, Fall Semester 2015 6 Binary ¡Numbers ¡ Bitwise ¡Operator ¡Examples ¡ • Recent ¡versions ¡of ¡Java ¡support ¡binary ¡literals: ¡ • 4-­‑bit ¡numbers: ¡ – Example: ¡0b11111111 ¡= ¡255 ¡decimal ¡ – 6 ¡& ¡5 ¡= ¡0b0110 ¡& ¡0b0101 ¡= ¡0b0100 ¡= ¡4 ¡ – Example: ¡255 ¡& ¡0b11110000 ¡= ¡240 ¡decimal ¡ – 6 ¡| ¡5 ¡= ¡ ¡0b0110 ¡| ¡0b0101 ¡= ¡0b0111 ¡= ¡7 ¡ • Integer ¡wrapper ¡class ¡can ¡print ¡binary ¡format: ¡ – 6 ¡^ ¡5 ¡= ¡0b0110 ¡^ ¡0b0101 ¡= ¡0b0011 ¡= ¡3 ¡ – System.out.println(“0b”+Integer.toBinaryString(255)); ¡ – ~6 ¡= ¡~0b0110 ¡= ¡0b1001 ¡= ¡9 ¡ 0b11111111 ¡ • 8-­‑bit ¡numbers: ¡ ¡ – 6 ¡<< ¡3 ¡= ¡0b00000110 ¡<< ¡3 ¡= ¡0b00110000 ¡= ¡48 ¡(6*8) ¡ – 48 ¡>> ¡4 ¡= ¡0b00110000 ¡>> ¡4 ¡= ¡0b00000011 ¡= ¡3 ¡(48/16) ¡ CS 160, Fall Semester 2015 7 CS 160, Fall Semester 2015 8 2

  3. Masking ¡Operadons ¡ Swapping ¡Values ¡ • You ¡cannot ¡exchange ¡values ¡without ¡using ¡a ¡ • Clearing ¡bits: ¡ temporary ¡variable: ¡ – x ¡= ¡0b00101001 ¡= ¡41 ¡ ¡ ¡int ¡temp ¡= ¡value0; ¡ – want ¡to ¡clear ¡top ¡4-­‑bits ¡ ¡ ¡value0 ¡= ¡value1; ¡ – x ¡= ¡x ¡& ¡0b00001111 ¡= ¡x ¡& ¡15 ¡= ¡0b00001001 ¡= ¡9 ¡ • Sefng ¡bits: ¡ ¡ ¡value1 ¡= ¡temp; ¡ – x ¡= ¡0b00101001 ¡= ¡41 ¡ ¡ ¡ – want ¡to ¡set ¡bogom ¡4-­‑bits ¡ – x ¡= ¡x ¡| ¡0b00001111 ¡= ¡x ¡| ¡15 ¡= ¡0b00101111 ¡= ¡47 ¡ CS 160, Fall Semester 2015 9 CS 160, Fall Semester 2015 10 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.isLeger(char ¡c); ¡ • Example ¡using ¡String ¡s ¡= ¡“HelloThere”; ¡ – Character.isSpace(char ¡c); ¡ – s.toUpperCase() ¡returns ¡“HELLOTHERE” ¡ • Example: ¡ – s.toLowerCase() ¡returns ¡“hellothere” ¡ – Character.isLeger(‘8’) ¡== ¡false ¡ – s.substring(2,7) ¡returns ¡“lloTh” ¡ – Character.isLowerCase(‘a’) ¡== ¡true ¡ CS 160, Fall Semester 2015 11 CS 160, Fall Semester 2015 12 3

  4. 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[]); ¡ • Example: ¡ – Arrays.equals(int ¡a1[], ¡int ¡a2[]); ¡ • 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 ¡excepdon ¡ – Arrays.sort(array) ¡sorts ¡array ¡to ¡{1, ¡2, ¡3, ¡4, ¡5} ¡ ¡ – Double.parseDouble(“There”) ¡gets ¡an ¡excepdon ¡ – Arrays.equals(array, ¡array) ¡= ¡true ¡ CS 160, Fall Semester 2015 13 CS 160, Fall Semester 2015 14 Date ¡Class ¡ Enumerated ¡Types ¡ • Methods ¡to ¡manipulate ¡dates ¡and ¡dmes: ¡ • General ¡Form: ¡ • Example ¡code ¡and ¡output: ¡ public ¡enum ¡DayOfWeek ¡{ ¡SUNDAY, ¡MONDAY, ¡ ¡ ¡ ¡TUESDAY, ¡WEDNESDAY, ¡THURSDAY, ¡FRIDAY, ¡ String ¡myFormat ¡= ¡”dd/MM/yyyy ¡(HH:mm:ss)”; ¡ ¡SATURDAY ¡ ¡} ¡ SimpleDateFormat ¡dateFormat ¡= ¡ • Example ¡Usage: ¡ ¡ new ¡SimpleDateFormat(myFormat); ¡ DayOfWeek ¡day ¡= ¡THURSDAY; ¡ Date ¡date ¡= ¡ new ¡Date(); ¡ switch ¡(day) ¡{ ¡ System.out.println(dateFormat.format(date)); ¡ ¡case ¡MONDAY: ¡ ¡ ¡ ¡case ¡TUESDAY: ¡ 19/11/2013 ¡(21:17:27) ¡ ¡… ¡ CS 160, Fall Semester 2015 15 CS 160, Fall Semester 2015 16 4

  5. Ternary ¡Operator ¡ • General ¡Form: ¡ (boolean ¡expression) ¡? ¡value1 ¡: ¡value2 ¡ • Example ¡Usage ¡(assume ¡age ¡is ¡integer): ¡ ¡ ¡ ¡ ¡ ¡double ¡price ¡= ¡(age ¡> ¡10) ¡? ¡30.0 ¡: ¡10.0; ¡ • Condidonal ¡Equivalent: ¡ ¡ ¡ ¡ ¡ ¡ double ¡price; ¡ if ¡(age ¡> ¡10) ¡price ¡= ¡30.0; ¡ else ¡price ¡= ¡10.0; ¡ CS 160, Fall Semester 2015 17 5

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