Friday, November 18, 2016

Java Constants[session6]

Numeric constants

All numbers are numeric constants

They are 2 types

Integer constant
decimal/real/floating-point constant.

Integer constant
Number without fractional part is called integer constant.

Example:

10
20
56

Decimal constant.
Number with fractional part is called decimal constant.

Example:

10.7
5.6

we can represent integer constant  in the following number systems.
i)Decimal(regular).
ii)Binary.
iii)Octal.
iv) Hexa decimal number systems.

Decimal number system
It has base as 10 i,e it consists base numbers from 0 to 9

Example:

  19
   20
   1
   158


Binary number system
It has base as 2 i,e it consists of digits 0 or 1.

Example:
   010
   110
   etc


Converting decimal to binary
If we want to convert decimal number to binary we should perform lcm using 2.

536   --> ?
  10      2

2|536
  ---
2|268-0
  ---
2|134-0
  ---
2|67-0
  --
2|33-1
  ----
2|16-1
  ----
2|8-0
  ---
2|4-0
  --
2|2-0
 ----
2|1-0
  ---
2|0-1


Collect  remainders from bottom to top

536-->1000011000

Converting binary to decimal
1    0   0   0    0     1   1    0    0    0
2^9 2^8 2^7  2^6  2^5  2^4  2^3  2^2  2^1  2^0


2^9*1+2^8*0+..........+2^0*0.

512+0+.......+0

536

Octal number system
It has a base as 8 i'e  it has digits from 0 to 7.
Always an octal integer must have a prefix 0[zero].

Example:
   0123(v)
   012(v)
   123(iv)
   078(iv)

Converting decimal to octal
36--> ?
10   8


8|36
  --
8|4-4
  ---
8|0-4

octal-->044

Converting octal to decimal
044-->     ?
            10

0   4    4
8^2 8^1  8^0


0+32+4-->36


Hexa decimal system
It has base as 16 i'e it consists altogether digits from 0 to 15

0 to 9[digits]

from 10-15 we should substitute alphabets a-f


10->a or A
11->b or B
12->c or C
13->d or D
14->e or E
15->f or F


It should have a prefix 0x or 0X


Example:

0x123
0xabc
0xface
etc

Converting decimal to hexa decimal

16|90
   --
16|5-10
   ----
16|0-5


ans->0x5A







No comments: