Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda ASCII char N int Character class char N String Bitwise operators homework.

Similar presentations


Presentation on theme: "Agenda ASCII char N int Character class char N String Bitwise operators homework."— Presentation transcript:

1 Agenda ASCII char N int Character class char N String Bitwise operators homework

2 ASCII codes ASCII is an acronym for American Standard Cod for Information Interchange All characters type are stored as numbers. Characters like 0…9, all upper/lower case letters, special symbols like,{,}, _, -, ?, : ; ) Refer to BPJ Appendix D for more information CharacterASCIICharacterASCII A65Y89 B66Z90

3 Why do we need ASCII code? Earlier we had EBSIDIC(Extended Binary Code Decimal Interchange Code) codes that was capable of accommodating English Letters, Punctuations and Digits. Then came the need of accommodating European alphabets and mathematical signs, box drawing characters... so ASCII was formed which could accommodate 256 codes. Now Unicode is introduced so that we can accommodate the alphabets of all the languages in the world and additional signs and symbols

4 What is Unicode? Programs are written using the Unicode character set. The Java programming language represents text in sequences of 16-bit code units, using the UTF-16 encoding. Except for comments, identifiers, and the contents of character and string literals, all input elements in a program are formed only from ASCII characters

5 How to convert a character to ascii? char aChar = ‘A’; int asciiCode = (int)aChar; How to convert an ascii to a character? int aNum = 65; char aChar= (char)aNum;

6 illegal statements Charater type char and String types can’t be stored into each other. char ch = “A”; String Str = ‘A’; or String str = “B”; char ch=str;

7 Surprising legal statements int and char can be added and store into int variable int x = 10; char y = ‘a’; int z = x + y; What z will be?

8 Is this legal? int aInt = 10; char aChar = ‘a’; aInt = aChar; => ok? aInt = ‘a’; => ok? aInt = 10 + aChar; => ok? int aInt = 10; char aChar = aInt;=> ok? aChar=(int)aInt; char aChar = 10;=> ok?

9 You can do this Look at ASCII chart, guess what will be the output for the following statement. char aChar = ‘A’; //65 int num = aChar + 10; //75 char bChar = (char)num; what is bChar?

10 Character class Provides many methods isAlphabetic() isDigit() isLowerCase() isUpperCase()return type?? isWhiteSpace() toUpperCase() toLowerCase() How to call the method?

11 char N String char aChar = ‘A’ ; String aStr = “B” ; aChar = aStr; => ok? aStr = aChar; => ok? aStr = aChar + “ “ ; => ok Conversion from char to string. Conversion from string to char aChar = aStr.CharAt(0); aChar = aStr.CharAt(1); => ok?

12 Bitwise operators Applied to binary only Boolean operators- && || Bitwise operators- & | Bitwise AND - & 011 001

13 Bitwise OR : | 011 001 011 Bitwise exclusive OR: (XOR) ^ 011 001 010 Short circuit evaluation: the left operand is evaluated first. If the result of the entire expression can be determined by the value of the left operand, then no other operands will be evaluated

14 Bitwise NOT- ~ 0 => 11 => 0 AND (90 10 & 107 10 ) 90 10 0 1 0 1 1 0 1 0 107 10 0 1 1 0 1 0 1 1 0 1 0 0 1 0 1 0 OR (90 10 | 107 10 ) 90 10 0 1 0 1 1 0 1 0 107 10 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 1

15 XOR ^ (90 10 ^ 107 10 ) 90 10 0 1 0 1 1 0 1 0 107 10 0 1 1 0 1 0 1 1 0 0 1 1 0 0 0 1 NOT ~ 46 00000000 00000000 00000000 00101110 11111111 11111111 11111111 11010001 most significant bit(msb) 0 – positive 1 - negative

16 Bitwise operations Decimal shifting to the right 283.0 -> 28.3 dividing by 10 Decimal shifting to the left 283.0 -> 2830 multiplying by 10 Binary shifting to the right: >> 32 >> 3 shift the bits(representing number 7) to the right 3 times 2*2*2=8 32/8 = 4 Binary shifting to the left: << 7 << 2 shift the bits to the left 2 times

17 A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, primitive action directly supported by the processor, and is used to manipulate values for comparisons and calculations. On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than additionbit patternsbinary numeralsbits processor

18 Homework Preview BPJ 31, not 27 as specified in lesson plan; Barron’s p166-169 Do all the exercises on BPJ 13 Convert a String to an array of characters. 1.Hard code the text to a variable : “ Programs are written using the Unicode character set. The Java programming language represents text in sequences of 16-bit code units, using the UTF-16 encoding. ” 2.Declared an char array with the size of the text. 3.Using a for loop and charAt(..) to store each symbol(letter, space, period..) into the char array 4.Use enhanced for loop to print out the char array


Download ppt "Agenda ASCII char N int Character class char N String Bitwise operators homework."

Similar presentations


Ads by Google