Number Systems and Data Representation
Number Systems and Data Representation
REMAINDER
2 25
2 12 1 LSB
2 6 0
2 3 0
1 1
MSB
© Copyright Reserved by Debanjan Gupta
REMAINDER
2 85 1
2 42 0
2 21 1
2 10 0
2 5 1
2 2 0
1
Now, put both the values together (integral and decimal part together) to get the required
binary equivalent. The binary digits of integral part is to be arranged from bottom to top
and vice-versa for the fractional part.
Thus, (85.27)10 = (1010101. 01000)2 approx.
2. DECIMAL TO OCTAL
To, convert a decimal number to octal, follow the same procedure as in case of decimal to
binary, only instead to 2 the decimal number should be divided by 8
© Copyright Reserved by Debanjan Gupta
8 395 3
8 49 1
6
(395)10 = (613)8
3. DECIMAL TO HEXADECIMAL
To, convert a decimal number to hexadecimal, follow the same procedure as in case of
decimal to octal, only instead to 8 the decimal number should be divided by 16
© Copyright Reserved by Debanjan Gupta
16 2699 11(B)
16 168 8
10(A)
1 1 0 1 0 1 Binary
Number
25 24 23 22 21 20 Power
32 16 8 4 2 1 Value
1 0 1 0 1 . 1 0 1 Binary
Number
24 23 22 21 20 . 2-1 2-2 2-3 Power
16 8 4 2 1 . 0.5 0.25 0.125 Value
2. BINARY TO OCTAL:
Binary to Octal Conversion can also be done using the above table:
Ex: Convert (1100101)2 to Octal
HEXADECIMAL BINARY
DIGITS EQUIVALENT
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
© Copyright Reserved by Debanjan Gupta
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
4 1 0 5 Octal Number
83 82 81 80 Power
512 64 6 1 Value
5 4 1 2 . 3 5 Octal
Number
83 82 81 80 . 8-1 8 -2 Power
512 64 8 1 . 0.125 0.015625 Value
3 = 011
1 = 001
2 = 010 Therefore, (312)8 = (011001010)2
4 = 100 2 = 010
3 = 011 4 = 100
5 = 101 1 = 001
7 = 111 Therefore, (4357.241)8 = (100011101111. 010100001)2
3. OCTAL TO HEXADECIMAL
Ex: Convert (342.41)8 to Hexadecimal:
3 4 2 . 4 1
0000 = 0
1110 = E
0010 = 2
.
1000 = 8
0100 = 4
2. HEXADECIMAL TO BINARY
HEXADECIMAL BINARY
DIGITS EQUIVALENT
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
D 9 7 Hexadecimal
Number
13 9 7 Hexadecimal Value
1101 1001 0111 Binary Equivalent
CODING:
Write a menu driven to:
1. Convert decimal to binary
2. Convert binary to decimal
Based on user’s choice.
# USING JAVA:
import java.util.*;
class NumberConversion{
void decToBinary(int decimal) {
int[] binary = new int[1000];
int i = 0,copy=decimal;
while (decimal > 0) {
binary[i] = decimal % 2;
decimal /= 2;
i++;
}
System.out.println("The Binary equivalent of "+copy+" is:");
for(int j=0;j<i;j++){
System.out.print (binary[j]);
}
System.out.println();
}
while (binary != 0) {
remainder = binary % 10;
binary /= 10;
decimalNumber += remainder * Math.pow(2, i);
++i;
}
System.out.println("The Decimal Equivalent of "+copy+" is
"+decimalNumber);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
switch (choice) {
case 1:
System.out.println("Enter a Decimal NUmber");
int dec = sc.nextInt();
obj.decToBinary(dec);
break;
case 2:
System.out.println("Enter a Binary Number");
int bin = sc.nextInt();
© Copyright Reserved by Debanjan Gupta
obj.binaryTodec(bin);
break;
default:
System.out.println("INVALID CHOICE");
}
}
}
}
#USING PYTHON
#PYTHON
def decimal_to_binary(decimal_number):
return bin(decimal_number)[2:]
def binary_to_decimal(binary_number):
return int(binary_number, 2)
while True:
print("Choose an option:")
print("1. Convert decimal to binary")
print("2. Convert binary to decimal")
print("3. Exit")
choice = int(input())
if choice == 1:
decimal_number = int(input("Enter a decimal number: "))
binary_number = decimal_to_binary(decimal_number)
print(binary_number)
elif choice == 2:
binary_number = input("Enter a binary number: ")
decimal_number = binary_to_decimal(binary_number)
print(decimal_number)
elif choice == 3:
print("Exiting program...")
break
else:
print("Invalid option")
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx