Академический Документы
Профессиональный Документы
Культура Документы
Bit Manipulation
Outline
21.1
21.2
21.3
21.4
21.5
21.6
21.7
Introduction
Vector Class and Enumeration Interface
Stack Class of Package java.util
Hashtable Class
Properties Class
Bit Manipulation and the Bitwise Operators
BitSet Class
21.1 Introduction
Utility classes and interfaces
Contained in package java.util
Class Vector
Interface Enumeration
Class Stack
Class Hashtable
Class Properties
Class BitSet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Outline
VectorTest.java
Line 10
Create Vector with initial
Lines 14,and
17 and 19
capacity of 10 elements
capacity increment of zero
Line 24
public VectorTest()
{
Vector vector = new Vector();
printVector( vector ); // print vector
// add elements to the vector
vector.add( "magenta" );
for ( int count = 0; count < colors.length; count++ )
vector.add( colors[ count ] );
vector.add( "cyan" );
printVector( vector ); // print vector
Line 25
Call Vector method add to add
objects to the end of the Vector
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Outline
Vector
method
contains
returns
// catch exception if vector is empty
that indicates whether
catch ( NoSuchElementException exception boolean
) {
exception.printStackTrace();
Vector contains a specific Object
VectorTest.java
Line 40
Lines 52-53
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Outline
VectorTest.java
Line 59elements
Vector method
returns Enumeration for
Line 64 elements
iterating Vector
}
System.out.println( "\n" );
}
public static void main( String args[] )
{
new VectorTest(); // create object and call its constructor
}
} // end class VectorTest
Outline
vector is empty
Size: 4
Capacity: 10
VectorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Outline
StackTest.java
Line 9
public StackTest()
{
Stack stack = new Stack();
// create objects to store in the stack
Boolean bool = Boolean.TRUE;
Character character = new Character( '$' );
Integer integer = new Integer( 34567 );
String string = "hello";
// use push
stack.push(
printStack(
stack.push(
printStack(
stack.push(
printStack(
stack.push(
printStack(
method
bool );
stack );
character );
stack );
integer );
stack );
string );
stack );
Create empty
Stack
Lines
18, 20, 22 and
24
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Outline
StackTest.java
while ( true ) {
removedObject = stack.pop(); // use pop method
System.out.println( removedObject.toString() + " popped" );
printStack( stack );
}
}
Line 32
Line 46
Line 51
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Outline
StackTest.java
Outline
hello popped
stack contains: true $ 34567
34567 popped
stack contains: true $
$ popped
stack contains: true
true popped
stack is empty
java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:79)
at java.util.Stack.pop(Stack.java:61)
at StackTest.<init>(StackTest.java:32)
at StackTest.main(StackTest.java:63)
StackTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Outline
WordTypeCount.j
ava
Line 21
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Outline
WordTypeCount.j
ava
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Outline
method
// create table from user Hashtable
input
private void createTable()
{
containsKey
determines
String input = inputField.getText();
method get obtains
whether the keyHashtable
specified as an
StringTokenizer words = new StringTokenizer(
input, "
\n\t\r"
);
Object associated
with
key from
WordTypeCount.j
ava
Line 66
} // end while
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
Outline
WordTypeCount.j
ava
Line 83
Hashtable method keys returns an
returns
Enumeration of keys inLine
the hash
93 table
Hashtable
method
isEmpty returns
the number
of key-value
pairs
that indicates whether
the key-value pairs
in boolean
the hash table
Line 94
currentKey + "\t" Hashtable
+ table.get( currentKey
) +Objects
"\n";
contains any
100
101
102
103
104
105
106
Outline
WordTypeCount.j
ava
Preferences API
Replace Properties
More robust mechanism
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Outline
PropertiesTest.
java
Line 20
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Outline
PropertiesTest.
java
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Outline
Lines 64-65
if ( value == null )
showstatus( "Put: " + nameField.getText() +
" " + valueField.getText() );
else
showstatus( "Put: " + nameField.getText() + " " +
valueField.getText() + "; Replaced: " + value );
listProperties();
}
} // end anonymous inner class
); // end call to addActionListener
// button to empty contents of Properties table
JButton clearButton = new JButton( "Clear" );
southPanel.add( clearButton );
clearButton.addActionListener(
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
Outline
PropertiesTest.
java
Lines 113-114
Properties method
getProperty locates value
associated with the specified key
if ( value != null )
showstatus( "Get property: " + nameField.getText() +
" " + value.toString() );
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
else
showstatus( "Get: " + nameField.getText() +
" not in table" );
Outline
listProperties();
PropertiesTest.
java
}
} // end anonymous inner class
); // end call to addActionListener
// button to save contents of Properties table to file
JButton saveButton = new JButton( "Save" );
southPanel.add( saveButton );
saveButton.addActionListener(
new ActionListener() { // anonymous inner class
// use method save to place contents in file
public void actionPerformed( ActionEvent event )
{
// save contents of table
try {
FileOutputStream output =
new FileOutputStream( "props.dat" );
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
Outline
Line 147
}
} // end anonymous inner class
); // end call to addActionListener
// button to load contents of Properties table from file
JButton loadButton = new JButton( "Load" );
southPanel.add( loadButton );
loadButton.addActionListener(
new ActionListener() { // anonymous inner class
// use method load to read contents from file
public void actionPerformed( ActionEvent event )
{
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Outline
PropertiesTest.
java
Properties method
load
Line 179
restores Properties contents
from FileInputStream
}
} // end anonymous inner class
); // end call to addActionListener
container.add( southPanel, BorderLayout.SOUTH );
setSize( 550, 225 );
setVisible( true );
} // end constructor
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
Outline
PropertiesTest.
java
Line 207
while ( enumeration.hasMoreElements() ) {
name = enumeration.nextElement().toString();
value = table.getProperty( name );
buffer.append( name ).append( '\t' );
buffer.append( value ).append( '\n' );
}
Properties method
propertyNames obtains
Enumeration of property names
displayArea.setText( buffer.toString() );
}
// display String in statusLabel label
public void showstatus( String s )
{
statusLabel.setText( s );
}
public static void main( String args[] )
{
PropertiesTest application = new PropertiesTest();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class PropertiesTest
Outline
PropertiesTest.
java
Program Output
Operator
&
Name
bitwise AND
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
PrintBits.java
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Outline
PrintBits.java
Lines 31-32
Convert String to int, then pass
Line
55 getBits
int to private
method
to get the ints bit representation
} // end constructor
// display bit representation of specified int value
private String getBits( int value )
{
// create int value with 1 in leftmost bit and 0s elsewhere
int displayMask = 1 << 31;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Outline
PrintBits.java
Line 63
Line 65
return buffer.toString();
} // end method getBits
public static void main( String args[] )
{
PrintBits application = new PrintBits();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class PrintBits
Outline
PrintBits.java
Program Output
Bit 1
0
1
0
1
Fig. 21.7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Outline
MiscBitOps.java
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Outline
MiscBitOps.java
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Outline
Lines 66 and 67
Lines 86 and 87
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
Outline
MiscBitOps.java
exclusiveOrButton.addActionListener(
new ActionListener() { // anonymous inner class
Lines 106
107
OR and
(^) to
// perform bitwise exclusive OR and display results Use bitwise exclusive
public void actionPerformed( ActionEvent event )
combine value1 and value2
{
setFields();
resultField.setText( Integer.toString( value1 ^ value2 ) );
bits3Field.setText( getBits( value1 ^ value2 ) );
}
} // end anonymous inner class
); // end call to addActionListener
// button to perform bitwise complement
JButton complementButton = new JButton( "Complement" );
buttonPanel.add( complementButton );
complementButton.addActionListener(
new ActionListener() { // anonymous inner class
// perform bitwise complement and display results
public void actionPerformed( ActionEvent event )
{
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
input2Field.setText( "" );
bits2Field.setText( "" );
Outline
MiscBitOps.java
Lines 130 and 132
}
} // end anonymous inner class
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
Outline
MiscBitOps.java
Outline
MiscBitOps.java
Program Output
Bit 1
0
1
0
1
Fig. 21.9
Bit 1 | Bit 2
Bit 2
0
0
0
1
1
1
1
1
Bitwise inclusive OR operator (|) combining two bits.
Bit 1 ^ Bit 2
Bit 1
Bit 2
0
0
0
1
0
1
0
1
1
1
1
0
Fig. 21.10 Bitwise exclusive OR operator (^) combining two bits.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Outline
BitShift.java
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Outline
BitShift.java
Line 56
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Outline
BitShift.java
Line 77
Use bitwise signed right-shift
(>>) to shift values bits to
the right by one position
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Outline
BitShift.java
Line 98
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Outline
BitShift.java
Program Output
Outline
BitShift.java
Program Output
Bitwise
assignment
operators
Bitwise AND assignment operator.
&=
Bitwise inclusive OR assignment operator.
|=
Bitwise exclusive OR assignment operator.
^=
Left-shift assignment operator.
<<=
Signed right-shift assignment operator.
>>=
Unsigned right-shift assignment operator.
>>>=
Fig. 21.12 Bitwise assignment operators.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
BitSetTest.java
Line 18
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Outline
BitSetTest.java
inputField.addActionListener(
new ActionListener() { // inner class
// determine whether value is prime number
public void actionPerformed( ActionEvent event )
{
int value = Integer.parseInt( inputField.getText() );
if ( sieve.get( value ) )
statusLabel.setText( value + " is a prime number" );
else
statusLabel.setText( value + " is not a prime number" );
}
} // end inner class
); // end call to addActionListener
JTextArea primesArea = new JTextArea();
container.add( new JScrollPane( primesArea ), BorderLayout.CENTER );
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Outline
BitSetTest.java
Lines 59-60
Lines 63-70
Determine prime numbers
86
87
88
89
90
91
92
Outline
BitSetTest.java