Title: Enumerated Types, Bitwise Operators
1Enumerated Types,Bitwise Operators
2A Simple Task Adding Attributes to a Shape
- Prior to Java Version 1.5
public class Shape public static final int
SMOOTH 0 // public static final int DOTS
1 // public static final int STRIPES 2
// private int _pattern public Shape(int
pattern) _pattern pattern . . .
3Declaring Enumeration
- Java 1.5 supports enumeration
public class Shape public enum Pattern
SMOOTH, DOTS ,STRIPES private Pattern
_pattern public Shape(Pattern pattern)
_pattern pattern . . .
4Using Enumeration
- A simplified example - inside class Shape
public String getTextureString() throws
Exception switch (_pattern) case
SMOOTH return "smooth" case DOTS
return "dots" case STRIPES
return "stripes" // The following code
should never be reached throw new
Exception(Unknown pattern")
5Enumerated Types
- Classic usage Represent a fixed set of
constants (as in C and C) - Natural sets e.g. days of the week, months,
shape fill patterns.. - Other setse.g. status of the program, input
flags, menu items - Enumerated types are actually classes whose
instances are static final objects - Therefore, an enum can have data members and
methods
6Adding Members and Methods
public enum Operator PLUS("", 2 , 3
), MULT("", 2 , 4 ), POW("", 2 , 5
), LOG("log", 1 , 6 ) private
final String _token private final int
_numArgs private final int _precedence . . .
7Members and Methods cont.
Operator ( String token, int numArgs, int prec
_token token _numArgs numArgs
_precedence precedence public String
getToken() return _token . . .
- PLUS, MULT, POW and LOG are constant objects of
enum type Operator
8Using Enumeration - Revisited
public class TestOperator public static void
main(String args) Operator op
Operator.MULT System.out.printf("s has d
arguments\n", op.toString(),
op.getNumArgs()) for ( Operator o
Operator.values() ) System.out.printf("s
precedence 6d\n", o.getToken(),
o.getPrecedence() )
9Back to Shapes -Abstract Methods
public enum Pattern SMOOTH String
getString() return "smooth" , DOTS
String getString() return "dots" ,
STRIPES String getString() return
"stripes" abstract String
getString()
10Primitive Data Types
Keyword Description Size/Format
Integers Integers Integers
byte Byte-length integer 8-bit twos complement
short Short integer 16-bit twos complement
int Integer 32-bit twos complement
long Long integer 64-bit twos complement
Real numbers Real numbers Real numbers
float Single-precision floating point 32-bit IEEE 754
double Double-precision floating point 64-bit IEEE 754
Other types Other types Other types
char A single character 16-bit Unicode character
boolean A boolean value (true or false) true or false
11Example int structure
- Big-endian the most significant value is stored
at the lowest storage address. - Twos complement
- The most significant bit is a sign bit
- Value of an n-bit binary number
- Example representation of the number 1025
content 00000000 00000000 00000100 00000001
address 0x00 0x01 0x02 0x03
12Logical Operators
Operator Use Operation
op1 op2 Bitwise AND
op1 op2 Bitwise OR
op1 op2 Bitwise exclusive OR (XOR)
op1 Bitwise complement
http//java.sun.com/docs/books/tutorial/java/nutsa
ndbolts/bitwise.html
13Operation of Bitwise Operators
Binary operators
Complement
Input Input Output Output Output
bit 1 bit 2 AND OR XOR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Inp Out
0 1
1 0
Example
int op1 12 // 1100 int op2 6 // 0110
int result ( op1 op2 ) // should be 0101
14Example - Bitwise Flags
public class BitwiseDemo static final int
VISIBLE 1 static final int DRAGGABLE 2
static final int SELECTABLE 4 static final
int EDITABLE 8 public static void
main(String args) int flags 0
flags flags VISIBLE flags flags
DRAGGABLE if ((flags VISIBLE) VISIBLE)
...
http//java.sun.com/docs/books/tutorial/java/nutsa
ndbolts/bitwise.html
15Shift Operators
Op Use Description
ltlt op1 ltlt op2 Shifts bits of op1 left by distance op2 fills with 0 bits on the right side
gtgt op1 gtgt op2 Shifts bits of op1 right by distance op2 fills with highest (sign) bit on the left side
gtgtgt op1 gtgtgt op2 Shifts bits of op1 right by distance op2 fills with 0 bits on the left side
http//java.sun.com/docs/books/tutorial/java/nutsa
ndbolts/bitwise.html
16Reading Integers from a Byte Array
public class ReadIntDemo public static void
main(String args) byte a new
byte4 a0 0 a1 0
a2 1 // 00000001 a3 -1 //
11111111 int s 0 s s (
unsignedByteToInt(a0) ltlt 24 ) s s
( unsignedByteToInt(a1) ltlt 16 ) s s
( unsignedByteToInt(a2) ltlt 8 ) s s
unsignedByteToInt(a3)
System.out.println(s) public static
int unsignedByteToInt(byte b) return
(int) b 0xFF