In C, why is 123 - 0123 != 0????

Status
Not open for further replies.

aditya.shevade

Console Junkie
Hi

I am wondering about the fact that in C, when you print 123 - 0123, the answer is -40 and not 0, similarly 0123 - 123 = 40. Why?

Aditya
 
R

rahul_becks23

Guest
what do u initialize the values with ....... an int , double .....etc ..... which one ???
 

Sykora

I see right through you.
0123 invokes C's representation of octal numbers, as the default way of denoting a number as octal (as opposed to decimal) is to affix a leading 0. So 0123 is actually 64 + 16 + 3 = 83, and so 123 - 83 = 40, 83 - 123 = -40.
 

subhajitmaji

Broken In
Sykora said:
0123 invokes C's representation of octal numbers, as the default way of denoting a number as octal (as opposed to decimal) is to affix a leading 0. So 0123 is actually 64 + 16 + 3 = 83, and so 123 - 83 = 40, 83 - 123 = -40.
Absolutely Right
 

casanova

The Frozen Nova
Skyora, I new the answer but confused with ur interpreatation of 0123 as 64+16+3
Whats the trick behind this.
 
OP
aditya.shevade

aditya.shevade

Console Junkie
It's 123 in octal. Means
Code:
(3 * (8**0)) + (2 * (8**1)) + (1 * (8**2))

resulting in
Code:
3 + 16 + 64

Same as we do in decimals, 123 is
Code:
3 * 10**0 + 2 * 10**1 + 1 * 10**2

Logic is the same as that of binary. Go on increasing the power of base of number system by 1 as you move from right to left.

So hex ab is
Code:
b ie 11 * (16**0) + a ie 10 * (16**1)

equal to
Code:
 11 + 160 = 171
in decimal system.

Aditya
 
R

rahul_becks23

Guest
thanks for the info .......... but i guess i'll have to read abt that somewhere in more detail .
 

Sykora

I see right through you.
aditya.shevade said:
what to do if you want to convert the number back to decimal or hex, bin anything?

You can write your own function, which takes the number, successively extracts digits out of it, and reassembles the number in the new base. Obviously if the base is more than ten, you'll need to accept it as a string.

That said, there should probably already be solutions out there.
 
OP
aditya.shevade

aditya.shevade

Console Junkie
^^ Ya I know that. I am asking if there is any inbuilt function (like showbits(); ).... or maybe I might find header files somewhere.....

Otherwise making a program is not difficult at all...

Aditya
 

abhi_10_20

Cool and Calm
Another one...

printf("%d", 32<<-1);

answer it gives is 0...dunno how......

the operator is actually the left-shift operator which multiplies an unsigned int by 2, i.e if its :

printf("%d", 32<<1);

it gives 64, but with -1, how's it 0????
 
OP
aditya.shevade

aditya.shevade

Console Junkie
Are you sure that is multiplies the int by 2? Or does it add int*1 to int? In that case, 32 + 32*-1 = 0.... It's just a theory, I have no idea about the real reason......

Aditya
 

abhi_10_20

Cool and Calm
it actually left shifts the bits in binary form of 32,

i.e. 32 in binary =100000;

when its bits are left shifted by 1, it becomes 1000000 that is 64 in decimal..

but, dunno how, with -1 it becomes 0.......
 
Status
Not open for further replies.
Top Bottom