Converting between decimal number representation
and binary number representation
Problem Description: we
have
2 different representations for
(unsigned) numerical values:
How do we
convert between
these 2 different
representations ???
Why you
must learn
conversion between decimal and binary numbers
-
Humans are
accostumed to use the
decimal number system to
represent
integer (= whole) values
-
In contrast:
computer
must use
the
binary number system...
- Computer Science professionals
must
know the
tool of their trade (= computer)
|
- Therefore:
-
Converting between
binary
⇆ decimal numbers
a
fundamental skill
in Computer Science
|
Examples:
Given: 10101 in binary --> the decimal number
for the same value = ???
Given: 25 in decimal --> the binary number
for the same value = ???
|
|
How to
convert from
binary representation ⇒
decimal representation
-
To convert the
binary representation to
the decimal representation
(for the same value),
do the following:
- Compute the
decimal sum of
the value of each
(binary) digit
multiplied by its
"position factor"
|
- Example: Convert the
binary number
01011001 to
a decimal number
Worked out
example:
Digits: 0 1 0 1 1 0 0 1
Multiply factor: 27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
--------------------------------
64 + 16 +8 + 1 = 89
Answer: 01011001 binary = 89 decimal
|
|
How to
convert from
decimal
representation ⇒
binary
representation
Why programming languages
have
different integer
data types
Storing
integer (= whole) values inside a computer program
Important note: the
computer will
only use these
fixed
lengths to
store
integer (= whole) numbers:
- byte:
uses 1 byte of memory
(i.e.: 8 bits binary number)
- short:
uses 2 bytes of memory
(i.e.: 16 bits binary number)
- int:
uses 4 bytes of memory
(i.e.: 32 bits binary number)
- long:
uses 8 bytes of memory
(i.e.: 64 bits binary number)
|
Reminder: the
computer
must
store the
integer (= whole) numbers
as
binary numbers
Storing integer (whole) values inside a computer program
How the
computer store
integers as
fixed length
binary numbers:
- The (decimal) value 5
represented (= stored) as
the byte
data type is:
- The (decimal) value 5
represented (= stored) as
the short
data type is:
- The (decimal) value 5
represented (= stored) as
the int
data type is:
00000000000000000000000000000101
|
|
So the
leading zeros in a
binary number is
also
stored in
memory !!!
Quiz 1 - Applying
what you have learned so far
- What bit pattern is
stored
in
memory variable
x:
|
Quiz 1 - Applying
what you have learned so far
- What bit pattern is
stored
in
memory variable
x:
Answer:
Convert 6 to binary: 6
2 ----- 0
3
2 ----- 1
1
2 ----- 1
0
The binary number is: 110 binary
Because x is a byte variable x contains: 00000110
|
|
Quiz 2 - Applying
what you have learned so far
- What bit pattern is
stored
in
memory variable
x:
|
Quiz 2 - Applying
what you have learned so far
- What bit pattern is
stored
in
memory variable
x:
Answer:
The binary number is: 110 binary
Because x is a int variable x contains:
00000000000000000000000000000110
(32 bits)
|
|
❮
❯