Java Assignment I: Bitwise Operators Bitwise Shift Operators Garbage Collection Class Buffered Reader

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

JAVA

ASSIGNMENT I
Bitwise Operators
Bitwise Shift Operators
Garbage Collection
Class Buffered Reader

H.E.Pavithra Mayuri

0210GP
Bitwise Operators
Bitwise Operators are AND, OR, XOR and NOT and the result is always true or false.

 The examples here use 8-bit integers, Java integers are 32 bits. The highest bit is
reserved for plus or minus sign. So you can set/unset 31 one-bit flags and a whole
bunch of combination masks derived from the one-bit flag masks.

 It is not recommended to use bitwise operations in routine application


programming because the resulting code is not very easy to understand. The
flow of logic is not obvious. Save bitwise operations for systems programming
or where memory savings may be desired such as in a JavaCards.

 One hexadecimal digit represents four binary digits.

Logical Bitwise Operations


A B OR (|) AND (&) XOR (^)
0 0 0 0 0
1 0 1 0 1
0 1 1 0 1
1 1 1 1 0

AND Bitwise Operator (&)

A bitwise AND takes two binary representations of equal length and performs the logical
AND operation on each pair of corresponding bits. In each pair, the result is 1 if the first
bit is 1 AND the second bit is 1. Otherwise, the result is 0. (All inputs are high for the
high output)

Logic Symbol

A A.B
B

 This operator is used to check the state of a flag in your flags variable. When
you AND your flags variable with the MASK, all zeroes in the MASK will return
zero for the corresponding position in flags and all ones in the MASK will return
whatever the corresponding bit is set to in your flags variable. Therefore, the
bitwise AND operator's evaluates to the MASK itself only if the MASK flags are also
set in the flags variable.

Example -:
0101 (decimal 5)
AND 0011 (decimal 3)
= 0001 (decimal 1)
Example -: // To check the state of a flag bit(s)
if ((flags & MASK) == MASK) {
// flag is set or turned on
...}
else {
//flag is not set or is turned off
...}

OR Bitwise Operator ( | )
A bitwise OR takes two bit patterns of equal length, and produces another one of the same length
by matching up corresponding bits and performing the logical inclusive OR operation on each pair
of corresponding bits. In each pair, the result is 1 if the first bit is 1 or the second bit is 1 or both
bits are 1, and otherwise the result is 0. (Any input is high for the high output.)

Logic Symbol

A A+B

Examples -:

 The Bitwise XOR is used to toggle the flag bits of a MASK in your flags
variable. It means that, if a flag bit was set in flags, XORing with its MASK will
unset it. If it was not set, XORing will set it. This is different from turning a flag
bit on or off, regardless of its prior state (which is accomplished using | and & ~).

// To toggle a flag bit(s) (on if it was off, off if it was on)


flags = flags ^ MASK;
// or, more succinctly
flags ^= MASK;

0101 (decimal 5)
OR 0011 (decimal 3)
= 0111 (decimal 7)

XOR Bitwise Operator (^)


A bitwise exclusive or takes two bit patterns of equal length and performs the logical XOR
operation on each pair of corresponding bits. The result in each position is 1 if the two bits are
different, and 0 if they are the same. (All inputs are same for the law output)

Logic Symbol

A A B

Example -:

 The Bitwise XOR is used to toggle the flag bits of a MASK in your flags
variable. It means that, if a flag bit was set in flags, XORing with its MASK will
unset it. If it was not set, XORing will set it. This is different from turning a flag
bit on or off, regardless of its prior state (which is accomplished using | and & ~).

// To toggle a flag bit(s) (on if it was off, off if it was on)


flags = flags ^ MASK;
// or, more succintly
flags ^= MASK;

Example -: 0101 (decimal 5)


XOR 0011 (decimal 3)
= 0110 (decimal 6)

NOT Bitwise Operator (~)

The bitwise NOT, or complement, is a unary operation that performs logical negation on each bit,
forming the ones' complement of the given binary value. Digits which were 0 become 1, and vice
versa. (The input is law for the high output)

Logic Symbol

A A’

Examples -:

 This is useful when "unsetting" or "turning off" a flag.

// To unset or turn off a flag bit(s)


flags = flags & ~MASK;
// or, more succintly
flags &= ~MASK;

NOT 0111 (decimal 7)


1000 (decimal 8)

Bitwise Shift Operators


Bitwise Shift Operators are Signed left shift(<<), Signed right shift (>>) and Unsigned right shift
(>>>). Bitwise shift operators divide into tree parts. These are Arithmetic shift, Logical shift and
Circular shift.

 The bit shifts are sometimes considered bitwise operations, since they operate on the
binary representation of an integer instead of its numerical value; however, the bit shifts
do not operate on pairs of corresponding bits, and therefore cannot properly be called bit-
wise operations.
 In this operation, the digits are moved, or shifted, to the left or right. Registers in a
computer processor have a fixed number of available bits for storing numerals, so some
bits will be "shifted out" of the register at one end, while the same number of bits are
"shifted in" from the other end; the differences between bit shift operators lie in how they
compute the values of those shifted-in bits.
Arithmetic Shift
In an arithmetic shift, the bits that are shifted out of either end are discarded. In a left In an
arithmetic shift, the bits that are shifted out of either end are discarded. In a left arithmetic shift,
zeros are shifted in on the right; in a right arithmetic shift, the sign bit is shifted in on the left, thus
preserving the sign of the operand. Further on while shifting right the empty spaces will be filled
up with a copy of the MSB. Meaning by shifting ASR#2 with a MSB=1 you fill up with 1. This
example uses an 8-bit register. In the first case, the leftmost digit was shifted past the end of the
register, and a new 0 was shifted into the

Left arithmetic shift


Right arithmetic shift

Logical Shift
In a logical shift, the bits that are shifted out are discarded, and zeros are shifted in (on either end).
Therefore, the logical and arithmetic left-shifts are exactly the same operation. However, the
logical right-shift inserts bits with value 0 instead of copying in the sign bit. Hence the logical shift
is suitable for unsigned binary numbers, while the arithmetic shift is suitable for signed two's
complement binary numbers.

Logical shift right Logical shift left

Circular Shift

Another form of shift is the circular shift or bit rotation. In this operation, the bits are
"rotated" as if the left and right ends of the register were joined. The value that is shifted in
on the right during a left-shift is whatever value was shifted out on the left, and vice versa.
This operation is useful if it is necessary to retain all the existing bits, and is frequently
used in digital cryptography.

Left rotate through carry


Right circular shift or rotate
Garbage collection
In computer science, garbage collection (GC) is a form of automatic memory management. The
garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects
that are no longer in use by the program. Garbage collection was invented by John McCarthy
around 1959 to solve problems in Lisp. (Auto delete unused java objects)

The basic principles of garbage collection are:

1. Find data objects in a program that cannot be accessed in the future


2. Reclaim the resources used by those objects

Benefits

Garbage collection frees the programmer from manually dealing with memory deal
location. As a result, certain categories of bugs are eliminated or substantially reduced:

 Dangling pointer bugs, which occur when a piece of memory is freed while there are still
pointers to it, and one of those pointers is then used. By then the memory may have been
re-assigned to another use, with unpredictable results.
 Double free bugs, which occur when the program tries to free a region of memory that
has already been freed, and perhaps already been allocated again.
 Certain kinds of memory leaks, in which a program fails to free memory occupied by
objects that will not be used again, leading, over time, to memory exhaustion.

Some of these bugs can have security implications.

Disadvantages

Typically, garbage collection has certain disadvantages:

 Garbage collection consumes computing resources in deciding which memory to free,


reconstructing facts that may have been known to the programmer.
 The moment when the garbage is actually collected can be unpredictable, resulting in
stalls scattered throughout a session. Unpredictable stalls can be unacceptable in real-
time environments such as device drivers, in transaction processing, or in interactive
programs.
 Memory may leak despite the presence of a garbage collector, if references to unused
objects are not themselves manually disposed of.
 In virtual memory environments typical of modern desktop computers, it can be difficult
for the garbage collector to notice when collection is needed, resulting in large amounts
of accumulated garbage, a long, disruptive collection phase, and other programs' data
being swapped out.

Example -:

Object x = new Foo();


Object y = new Bar();
x = new Quux();
/* at this point, we know that the Foo object will
* never be accessed: it is syntactic garbage
*/
if(x.check_something()) {
x.do_something(y);
}
System.exit(0);

Class BufferedReader
public class BufferedReader

 Read text from a character-input stream, buffering characters so as to provide for


the efficient reading of characters, arrays, and lines.

 The buffer size may be specified, or the default size may be used. The default is
large enough for most purposes.

 In general, each read request made of a Reader causes a corresponding read request
to be made of the underlying character or byte stream. It is therefore advisable to
wrap a BufferedReader around any Reader whose read() operations may be costly,
such as FileReaders and InputStreamReaders.

Example -:

public BufferedReader(Reader in,
int sz)
Create a buffering character-input stream that uses an input buffer of the specified size.
Parameters:
in - A Reader
sz - Input-buffer size
Throws:
IllegalArgumentException - If sz is <= 0

Method in Class BufferedReader

You might also like