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

Number system in python

The document discusses various number systems including binary, octal, decimal, and hexadecimal, explaining their bases and conversion methods. It provides programming examples for converting between these systems, detailing how to convert binary to decimal, octal, and hexadecimal, and vice versa. Each section outlines the principles of the respective number system and includes sample code for practical implementation.

Uploaded by

codewizard368
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Number system in python

The document discusses various number systems including binary, octal, decimal, and hexadecimal, explaining their bases and conversion methods. It provides programming examples for converting between these systems, detailing how to convert binary to decimal, octal, and hexadecimal, and vice versa. Each section outlines the principles of the respective number system and includes sample code for practical implementation.

Uploaded by

codewizard368
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

slings and Numbey System

Number System
The antheoehe volue thod i's used fothe Quonity
and usecd in moking colrulations is cle Roed ag NUMBFRs. The
woiding sys tem fou cencoing numbers logirely using dlgis or
Synbols is clefinecl as nomber system
Number stem is a sy stem forlo cernoling numbers in direot uys
to repoesen numbers io chifeot osms
Types of Number Systern
D
Birary Number SysBern
2) OcHal Number Sys lern
3) Oerinal Number System
4) Hescaceimal Number Systern
Birony Numbe ysterm
A Number systern wrth base or rodior 2 is
binory Number system. koouon os
Binary t perimal
foz birany to decimal convers ion, the birory numbe uses
assigned to eoch bit pastion weyht
eq: bina leB a birory number be lo01, in odr to (onveot i into
cerimal
IK2+Ox2 +0x2'+ lx2° = a

= (ai0
pogrom
cdeP binary. to clecirmalCbinory):
clecimol=0

bu dlgit in reversed Cstr Cbinon)):


if clgit=='' or ch¡itz-'o':
cerimol += iot (digit)* (2 ** pooes)
powes +=1
else
point(*involdl 1oput")
re-tun derimal
poirll birory-to.derimal C1Doi))
output:

Binary o octal
Sist convest binors number to decirmal numba by ossigning
oPight o each bit.Afle that conve hat cdeojmgl nomber
between
b octol by divdiog it by $ unil we get the YemaindeY
(6-1)
eg: Led Hhe binory number be I0
l001

The ceoimol roresporcling to hs birory = 4


div1de init by
paogram
cleP binary..ocbl ( bina1y):
cerirmal- biroy- to.cletirnal (binany)
ocal
ohile decimol>o:
&)+ocol
ocbl str Cdeoimol %
clecirmal clecimal ll8
teuin octal

puind ( biroy- %. ocolCã l001)

Birary to Hescadecinal

first conveot the binary number to cdecimal number by assigmng


decirrol number by
weight o och binorg biB APer hal civide the
16 ancl obain the rermoindler.

y Led birory number be ioo101


: 37
clecinol fos this binory
1637
2 5

.: ((00101), : a05)),

Pogrom
deP bindy. to. hex Cbirany):
decimol- biray- to. derimal Cbinay)
hex. cocles- O12 34 667 8qAC CDE F"
hex. str- !!
while cecimol >o:
hex.str : hex. (ocdes clecimal %16+hex. sdr
clecimal Il: 16

return hex.str

point ( biroy-to. bex (100101 )


#ouput: 25

OcBal Numbe Sysdern


Octol Number Systern is one in cohich he base valae iss g
also
H uses &cigits ie 0-7 fos the crearlon oPocal Numbers, 1t is
apositioral Syslern ie wPlght is assignel roch
to position

octl o Binory
ocbl numbes ore (onvaded to binany numbars by replocing
each chgit oth a three- bit binory numb
Let te octel number be 123
Corresponcing thee bit binary %r toch cigi is
The
2 3
001 OlO ol =(iol 0o11),
(123), - Co1oo),

Progrom
deP ocbl. to. birory (ot tal ):
derimal octal. to.cecimal (ocBol)
binay-'
while cdecimal >o:
binary str (decimal l%2)+ binary
clecimal Il=2
relaro bi rary
point (ocol o. binory Ci23))
taut: 1010!1

ocal to Decimal
An ocol number con be ronvetecd ino Derimal by asigning
topighl o each position
9: LeB the ocel numbes be 123
Assihn oeight eoch pasihon = x&2x8'+ 3xeO
- 83

'. (23)g ($3

deP ocbl. to.cdecimal (ocal):


derimal= o
powpo = 0
ohile ocal >o:
clecirmal + (octal olo) * Cg** powes)
octol ll= lb
powes +=|
reBurn clecimal

poirt ( ocl_to-cacimol (123))


#output: 83

ocal do Hradecimal
Aocal number ton be ronveo ed ib a heorocecimol number
by tonue ting the number into is clecimal ondthen the number to
hexaclecimal
Led he octol number be 123
Corres po nding decimol $3

53

(23), =(63),s
po0gyom
deP octLo. her Coclal):
cdecimol: ocol. to-cerioal (atl)
her-rodes "01234 56789ABCbEF"
hex-str-I)
while decimal >o:
hex-str- hex-codes clecimal oo 1o +hex-str
decimal-16
retarn hex- str

poind (ocbl- to her C123))


Houtpul : 53

Derimol Number Sys tem


A pumber oith a bose vouP of 1o is ter medl as a Decimg l
numba sys tern ond i is repesented using digits betoeen Coo 4).
Hoe the pbre vaue is terrmed Rom righ! to lePl as Brst plore yolue
Called units, serond o the IePB as Tens , s0 on Hondred , thousond s, etr.
becimal to Birory
foa clecimal to binory conversion,the number is dindeel by
2 unil we get Ior oas the Hnal remolnder
eg: le the decimol Number be to
clivide wth 2

|10
2 1

deP clerimal to. binary Cdeimal ):


binory- str !
ohile clecimol >0
binary-str= sr Cderimal %2)+ binoy. str
cdecimal JIE2

retorn binory- st
point Cderimol.to-binory Clo))
tt output: 1o10

Derimal to Octal

for cerimol to octal (onvesionhe nomber is divickd by s unil


get a numbey betwPPn 0to 7 0s the Rool vemoinder
eg: Le he cerimol numbey be io
divide i wth &

o), =(12;
poogrom
de cecimol.to.octol (derimol):
octol.str:
wbile clecimol >o:
oclol.str - sth Cdeimal lo &) + tal. st
clerirmol I)= 8
retm octl. str

point ( cledinolto- ocol C1o))


#ouput: 12

Derirmal to Herodecimol
foz clecirmol to harodecimal (onvexslon,the number is dividad
by 16 unil we gedo number betwpen oto Qond CA lo F) as the
remainde.

e9: Lel he deirol number be lo


Divde i by lb
16|10
heocadecimol orrespondiny to iis A

poogrom
deP clerimalto bex (decimal):
hex-cocles "l2345678g ABCOEF}
her. str !
while clecimol >o:
hex. str- hex-rocdes Cdecimol ooJ5]+ hex-str
cecimol I1=16
reBurn hez-str
paint (decirnol. to her Cio))
# output: A

Hexodecimol Number Syslem


Anumbe wth a base oP l6b is rollel aherodmimol numbe
system. lH is repaspOted using numbers betwpm Ado f. As both numerit
cligt ond alpho beBs ore used in his sysem , i} is olso rolled on
alphaument system

Hexaderimal o Binary
The heraderi mal numbey is ronver tedl iob a Binary rumber
by replaring eoch hex cliit woh is binors PGutveleol
eg: Let tho hex number be FADE
torrespording binory Rr each hex
A

(FADE ),, =(1i10101101 10),

poogio
deP hex- to binary Chr.st):
clecimal: hex to binors Chex.Slr)
binany :'
while decimol >0:
binary sr (decimol %2)+ binory
decimal ||= 2
relurn binaiy
point Chexto. binory C'FADE))
#utput: 1))010o)110
Heoracleimol to becirpol

Hescoderimul numba is tonuated into cleriaal oumbr by ossgoing

e9: Let the hex be BED


Assign wegb) b oth her dlgit
2
= l)x16+ |4x16 + I3xl
=3053

(eED), Ga53),0

dle her-to.cleiool (her-st):


hex_rocdes "o1234567894 BcDEF"
cleimal0
powPo =0
for cigit in rwesedl Chex.s):
decimol += hex-rocles. indea (digid) * CI6 ** power)
power +=|

return cecimal
poind(her-to-deimol C"BED"))
#outpul:3063

Heracdeimol to Dcal

he hraceimol number is (ovaled to on ortol number by frst


(onvating hot numbar b its equivolent binoy nurnber ond hen to a
binary number to on brbl number.
eq: Let the haaceimol number be FAB
tores ponding hreo bit birory
A

thre bi! binony


7 6

cef her-to. octal (hey.sth):


clecimal ha. to- cletimal Cho-st)
ocol:
while cecimal >0
octal =sr (decimal ob 6) +octol
clerimol I=8

relun ocBol

point (ha.o. octel("CAB"))


touput :7653

You might also like