0% found this document useful (0 votes)
37 views

Object Oriented Programming in Java Binary and Hexadecimal Numeration and Logical Operations

This document provides an overview of binary and hexadecimal numeration systems and logical operations. It covers converting between decimal, binary, and hexadecimal numbers. Binary numbers use base-2 place value with digits of 0 and 1. Hexadecimal uses base-16 with digits 0-9 and A-F. The document includes exercises for students to practice converting numbers and identifying place values. It also describes logical operations that are basic topics in computer engineering and circuits.

Uploaded by

aqil_shamsi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Object Oriented Programming in Java Binary and Hexadecimal Numeration and Logical Operations

This document provides an overview of binary and hexadecimal numeration systems and logical operations. It covers converting between decimal, binary, and hexadecimal numbers. Binary numbers use base-2 place value with digits of 0 and 1. Hexadecimal uses base-16 with digits 0-9 and A-F. The document includes exercises for students to practice converting numbers and identifying place values. It also describes logical operations that are basic topics in computer engineering and circuits.

Uploaded by

aqil_shamsi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

J A V A L A B M A N U A L

OBJECT ORIENTED PROGRAMMING IN JAVA



Bi nar y and Hexadeci mal Numer at i on and
Logi cal Oper at i ons





L A B
1


T A B L E O F C O N T E N T S
1.1 Binary numeration, whole numbers
1.2 Binary fractions
1.3 Adding binary numerals
1.4 Hexadecimal
1.5 Logical operations

P U R P O S E
A single bit is a piece of information that may have one of two states, on or off, high or low, 1 or 0. While a
single bit may store only two patterns, a group of bits may store many more patterns. For example, eight
bits, or a byte, may store one of 256 different bit patterns. The digits 0 and 1 are also the two digits used in
the binary, or base 2, numeration system. By studying binary numeration, we can more easily talk about
how information, such as numbers, letters, pictures and sounds, is stored in a computer. Additionally, the
hexadecimal, or base 16, numeration system gives us a way of referring to a four bit pattern with a single
hexadecimal digit.
If you continue in computer science these topics will be more thoroughly discussed in a hardware course.
In computer engineering, these are basic topics in circuits.

T O P R E P A R E
Read Wu: Chapter 0 pages 2 - 7
Read through this laboratory session


T O C O M P L E T E L A B 0 1
Work in groups of two people. If there is an odd number of people in the lab, there may be a group
of three people. You are expected to be on time so that appropriate groups may be formed.
Each person in the group should keep his/her own answers to the questions. To get the most of this
shared experience, you should individually answer a question and then compare the answer with
your partner. If one of you does not understand a concept, the other should explain until the
concept or process is understood. If you can explain, you truly do understand. If you are unsure of
your answers, check with the lab tutor.




When you have finished this lab, see the lab tutor, who will give you an open note,
twenty point quiz. This will be your grade for Lab 01


M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y P A G E 1 . 1
J A V A L A B M A N U A L
1.1 BINARY NUMERATION WHOLE NUMBERS
In both class and the text book, you have seen that binary numerals use the digits 0 and 1 and are based
on grouping in groups of two. A place value diagram for a binary numeral using eight digits is
____ ____ ____ ____ ____ ____ ____ ____
128 64 32 16 8 4 2 1
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
high bit low bit
Ex er c i s e 1. 1 Count from 0 to 15 in binary, record the binary numerals










Binary to Decimal:
Given a binary numeral, to find the decimal numeral, add the place values that contain a one.
1101 = = 8 + 4 + 1 = 13
decimal binary decimal binary
0 8
1 9
2 10
3 11
4 12
5 13
6 14
7 15
1 1 0 1
8 4 2 1

11000011 = = 128 + 64 + 2 + 1 = 195 1 1 0 0 0 0 1 1
128 64 32 16 8 4 2 1

Decimal to Binary:
Given a decimal numeral, to determine the binary numeral, place a one in the place values that sum to
the number, fill in the remaining places with a zero.
9 = = 8 + 1 1001


140 = = 128 + 8 + 4 10001100
1 0 0 0 1 1 0 0
128 64 32 16 8 4 2 1

1 1 0 1
8 4 2 1


P A G E 1 . 2 M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y
J A V A L A B M A N U A L

Ex er c i s e 1. 2 Complete the charts.

decimal binary decimal binary
101101 17
10110 37
11111 127
1001001 198







Ex er c i s e 1. 3 Answer these questions.
A. The largest value that can be stored in 4 bits is __________________ 2 = __________ 10
The next number is __________________ 2 = __________ 10

B. The largest value that can be stored in 6 bits is __________________ 2 = __________ 10
The next number is __________________ 2 = __________ 10

C. The largest value that can be stored in 8 bits is __________________ 2 = __________ 10
The next number is __________________ 2 = __________ 10

Algorithms
An al gor thm i is a step by step process for solving a problem. For example, a recipe for baking a chocolate
case is an algorithm. In this first semester of computer science, you will learn how to write a computer
program, a set of instructions, or algorithm, that the computer follows in order to successfully complete a
task.
An algorithm for converting a decimal whole number to binary involves division by two. The decimal
numeration system groups in tens; the binary numeration system groups in twos. Read this example that
uses the algorithm before the algorithm is actually stated.
The process of grouping by twos is accomplished by dividing by two. To convert the decimal numeral 13 to
a binary numeral, repeatedly divide by 2.
1. 13 2 = 6 (groups of two) with a remainder of 1
2. 6 2 = 3 (groups of four) with a remainder of 0 (groups of two)
3. 3 2 = 1 (group of eight) with a remainder of 1 (group of four)
4. 1 2 = 0 (groups of sixteen) with a remainder of 1 (group of eight)

1 1 0 1
8 4 2 1

Notice that this process can be described with instructions that
are repeated many times until some condition is met.


M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y P A G E 1 . 3
J A V A L A B M A N U A L
The instructions that describe this process use this terminology from arithmetic:





The algorthim is written as English sentences and describes the process on the previous page. Instructions
1 and 2 are executed only once. Instructions a, b, c and d are executed repeatedly as long as the condition
in 3 is true. We call this a loop.
quotient

dividend
1
12

6
13 2


remainder
divisor
The algorithm to convert a whole decimal numeral into a binary numeral
1. The dividend is the decimal numeral to be converted.
2. Initially, n is 0. (n is the exponent of 2, 2
n
gives the place value.)
3. Repeat instructions a d while the dividend is not equal to 0
a. Divide the dividend by 2, finding a quotient and a remainder
b. In the binary numeral, the digit at place value 2
n

is the remainder
c. Increase the value of n by one.
d. The new value for the dividend is the quotient
l
o
o
p
Ex amp l e: Convert 1310 to a binary numeral.
n = 3 n = 2 n = 1 n = 0

1
0

0
1 2

1
2

1
3 2

0
6

3
6 2

1
12

6
13 2

1 1 0 1
2
3
2
2
2
1
2
0
START
STOP

Ex er c i s e 1. 4 Follow the algorithm to convert the indicated decimal numerals to binary. Clearly show
your work.
A. Convert the decimal numeral 18 to binary







P A G E 1 . 4 M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y
J A V A L A B M A N U A L

B. Convert the decimal numeral 51 to binary







1.2 BINARY FRACTIONS
In the decimal numeration system, the whole portion of a number is separated from the fractional portion
with a decimal point. To the right of the point, the place values continue the pattern established to the left
of the decimal point.
____ ____ ____
.
____ ____ ____ ____
100 10 1 1/10 1/100 1/1000 1/10000
10
2
10
1
10
0

10
-1
10
-2
10
-3
10
-4

Similarly, in the binary numeration system, the whole portion of a number is separated from the fractional
portion with a binary point.
____ ____ ____
.
____ ____ ____ ____
4 2 1 1/2 1/4 1/8 1/16
2
2
2
1
2
0

2
-1
2
-2
2
-3
2
-4
Binary fraction to decimal fraction
This process is the same that is used with whole numbers.
0.1011
2
represents 1/2 + 1/8 + 1/16 = 11/16 or 0.6875
101.101
2
represents 4 + 1 + 1/2 + 1/8 = 5 5/8 or 5.625

Ex er c i s e 1. 5 Complete the chart, converting the binary numerals to decimal numerals.

binary decimal
0.111
10.01
1011.1001



M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y P A G E 1 . 5
J A V A L A B M A N U A L
Decimal fraction to binary fraction
In the decimal numeration system, some fractions such as 3/4, 7/8, and 2/5 can be expressed as terminating
decimals 0.75, 0.875 and 0.4 respectively. However, other fractions, such as 1/6, 1/3 and 1/7, can only be
expressed as non terminating, repeating decimals.

1/6 = 0.1666 1/3 = 0.333 1/7 = 0.142857142857

This is also true when expressing fractions in base 2. The fractions 3/4 and 7/8 are expressed in binary as
0.11 and 0.111 respectively. But, though 2/5 is a terminating decimal fraction, it is a non terminating,
repeating binary fraction 0.01100110.
The algorithm in Section 1.1 converts a whole decimal numeral to a whole binary numeral by repeatedly
dividing by 2. In contrast, the algorithm to convert a fractional decimal numeral to a fractional binary
numeral uses repeated multiplication by 2.
The instructions that describe the process use this terminology from arithmetic:
0.4 multiplicand
x 2 multiplier
0.8 product
Note that when converting a mixed numeral such as 6 2/5, the whole portion is converted with the first
algorithm, and the fractional portion is converted using the second algorithm.
The algorithm to convert a decimal fraction into a binary fraction
1. The multiplicand is the fractional portion of the decimal numeral to be converted.
2. n is -1 (n is the exponent of 2, 2
n
gives the place value)
3. Repeat instructions a d while the multiplicand is not equal to 0 or more digits are needed to
determine the repeated pattern
a. Multiply the multiplicand by 2, finding a product
b. The whole number portion of the product is the binary digit that belongs at place value 2
n

c. The new value for the multiplicand is the fractional portion of the product
d. Decrease the value of n by one.
l
o
o
p

Ex amp l e Convert the decimal numeral 0.4 to binary.
n = - 1 n = - 2 n = - 3 n = - 4



.0 1 1 0
START

0. 4
x 2
0. 8

0. 8
x 2
1. 6

0. 6
x 2
1. 2

0. 2
x 2
0. 4

DONE The new
multiplicand is 0.4,
which is a repeat
multiplicand.
Therefore, the pattern
is established.
An s wer 0.4 = 0.01102

P A G E 1 . 6 M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y
J A V A L A B M A N U A L

Ex er c i s e 1. 6 Follow the algorithm to convert the indicated decimal numerals to binary. Clearly show
your work.
A. Using any method to convert the decimal numeral 3.25 to a terminating binary numeral.







B. Using the algorithm, convert the decimal numeral 0.7 to a repeating binary numeral.










1.3 ADDING BINARY NUMERALS
When adding decimal numerals there are 100 combinations of sums, assuming order is important. Since
there are only two binary digits, there are only four possible addtion combinations. These combinations
and their sums are
0 0 1 1
+ 0 + 1 + 0 + 1
0 1 1 10 2
With these addition facts and your knowledge of how to add decimal numerals, you should be able to add
using binary numerals. Add the columns from right to left. If the result is more than one digit record the
1's digit and carry the remaining digit to the next column to the left. In the example, the related decimal
addition problem is also recorded for verification purposes.

1 1
1 0 1 0 1 21
+ 1 1 1 0 0 + 28
1 1 0 0 0 1 49

carry bit
Ex amp l e


M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y P A G E 1 . 7
J A V A L A B M A N U A L
Ex er c i s e 1. 7 Complete the following addition problems. Next to each problem, rewrite it using decimal
numerals for verification.

A. 10
+ 10


B. 101
+ 11


C. 1010
+ 1111

D. 1011. 101
+ 110. 001



1.4 HEXADECIMAL NUMERATION
Because information in the computer is based on the binary numeration system, it is easy for a human to
make an error when transposing long bit streams such as 00101110001110011010011. If instead, each
group of four bits is assigned a single symbol, errors are less likely to be made. A group of four bits can be
arranged in 2
4
= 16 different bit patterns. Hexadecimal numeration, or base 16, uses sixteen different
digits, just as a base 2 uses two digits and base 10 uses ten digits. By universal agreement the 16 digits
are 0 9, and the first six letters of the alphabet, either a f or A F. The digit A represents ten, and F
represents fifteen.












Binary to hexadecimal
decimal binary hexadecimal decimal binary hexadecimal
0 0000 0 8 1000 8
1 0001 1 9 1001 9
2 0010 2 10 1010 A
3 0011 3 11 1011 B
4 0100 4 12 1100 C
5 0101 5 13 1101 D
6 0110 6 14 1110 E
7 0111 7 15 1111 F

Therefore, the bit pattern 001011100011100110101011 separated into 4 bit groups
0010 1110 0011 1001 1010 1011
can be written as 2E39AB
P A G E 1 . 8 M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y
J A V A L A B M A N U A L

Similarly the six-bit binary numeral 110101 can be converted to binary by viewing it as
11 0101 or 0011 0101, which is 35
16
Hexadecimal to binary
To convert 5B16 to binary, write each hexadecimal digit as a four bit binary numeral.
5B
16
= 0101 1011 If leading zeros are not needed, this is 1011011
2

Hexadecimal to decimal
As with all place value numeration systems, a place value chart for hexadecimal numerals is
____ ____ ____
256 16 1
16
2
16
1
16
0

3416 represents 3 16 + 4 1 = 52
AC16 represents 10 16 + 12 1 = 172
F516 represents 15 16 + 5 1 = 245




Counting
To count in hexadecimal, you do not need to do any conversions. Just follow the same pattern that you
follow when counting in base 10 and base 2.
In base 10, the largest two digit number is 99. 99 is followed by 100
In base 2, the largest two digit number is 11. 11 is followed by 100
In base 16, the largest two digit number is FF. FF is followed by 100

In base 16, counting from 9516 :
96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0

Ex er c i s e 1. 8 Answer the following questions.
A. Rewrite the byte 11001001 in hex: __________________________________________________

B. Rewrite the hex numeral 6D in binary: ______________________________________________

C. For each of the hexadecimal numerals, write the next numeral. This is a counting question.

N16 next N16 N16 next N16
F CF
C2 5F
CB FFF

M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y P A G E 1 . 9
J A V A L A B M A N U A L
D. Convert the base 16 numerals to base 2 and base 10.

a. 4316 = _______________________________2 = _____________________ 10

b. BD16 = _______________________________2 = _____________________ 10

E. Convert base 10 numerals to base 16 and base 2.

a. 110111002 = _____________16 = _____________________ 10

b. 111012 = _________________16 = _____________________ 10

F. Convert the base 10 numerals to base 16 and base 2.

c. 4510 = _____________________16 = ___________________________ 2

d. 16510 = _____________________16 = ___________________________ 2


1.5 LOGICAL OPERATIONS
Given two bits as operands, there are logical operations that result in a third bit. The operations AND,
OR, XOR, and NOT are called Boolean operations after the nineteenth century logician George Boole.
NOT is a unary operator, meaning that it is applied to a single operand that has one of two values, either
true or false. The other operations are applied to two operands and are, therefore, binary operands. For
the following examples, the operands are two statements P and Q, which are either true or false. Suppose

P: December 25 is Christmas Day is true
Q: February 13 is Valentine's Day is false
AND
The statement

AND False True
False F F
True F T


P

December 25 is Christmas Day AND February 13 is Valentine's Day

is false . That is, P AND Q is false.
In fact, P AND Q is true only if both P and Q are true.
Q

Let the binary digit 0 represent false and 1 represent true.
When using binary digits, the rule for computing P AND Q translates into

0 1 0 1
AND 0 AND 0 AND 1 AND 1
0 0 0 1




P A G E 1 . 1 0 M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y
J A V A L A B M A N U A L

OR
Now consider the logical operation OR. The statement

December 25 is Christmas Day OR February 13 is Valentine's Day

is true. That is, P OR Q is true.
In fact, P OR Q is only false if both P and Q are false.

When using binary digits, this translates into:
0 1 0 1
OR 0 OR 0 OR 1 OR 1
0 1 1 1


OR Fa;se True
False F T
True T T


P

Q






XOR
The XOR operation is read as exclusive or.
The statement P XOR Q is true if exactly one of P and Q is true.
If both P and Q are true, P XOR Q is false.

For example, the statement.

December 25 is Christmas Day XOR February 13 is Valentine's Day

XOR Fa;se True
False F T
True T F


P

Q


is true. But, the statement

December 25 is Christmas Day XOR February 14 is Valentine's Day

is false.

When using binary digits, this translates into:


0 1 0 1
XOR 0 XOR 0 XOR 1 XOR 1
0 1 1 0



NOT
There is one more logical operation that is applied to only one Boolean operand and that is NOT. NOT
negates the value of an expression. That is,

NOT (December 25 is Christmas Day) or rather December 25 is not Christmas Day

is false. That is, if P is true, NOT P is false. And, if P is false, NOT P is true. When using binary digits,
this translates into:
NOT 0 1 NOT 1 0

Each of these operations is implemented in a computer with a device called a gate. An AND, OR, or XOR
gate has two inputs and outputs one value, a NOT gate has one input and outputs one value. The
circuitry of a computer is built using these gates as the lowest level building blocks.

M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y P A G E 1 . 1 1
J A V A L A B M A N U A L
Ex er c i s e 1. 9
A. What is the result when AND is applied to these bytes?
10101010
AND 11110000




Gen er al i ze: If x is a bit, that is, either 0 or 1, then x AND 0 is _______ and x AND 1 is _________


B. What is the result when OR is applied to these bytes?
10101010
OR 11110000




Gen er al i ze: If x is a bit, that is, either 0 or 1, then x OR 0 is _______ and x OR 1 is _________


C. What is the result when XOR is applied to these bytes?
10101010
XOR 11110000




Gen er al i ze: If x is a bit, that is, either 0 or 1, then x XOR 0 is _______ and x XOR 1 is _________


P A G E 1 . 1 2 M A R I A N M A N Y O , M A R Q U E T T E U N I V E R S I T Y

You might also like