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



## aditya.shevade (Apr 2, 2007)

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


----------



## rahul_becks23 (Apr 2, 2007)

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


----------



## aditya.shevade (Apr 2, 2007)

^^ I tried 
	
	



```
int x=0123;
```
 then 
	
	



```
const int x=0123;
```


----------



## Sykora (Apr 2, 2007)

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 (Apr 2, 2007)

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


----------



## aditya.shevade (Apr 2, 2007)

Thank you very much Sykora.... no reps now, or you would have received one


----------



## Sykora (Apr 2, 2007)

It's the thought that counts.


----------



## casanova (Apr 2, 2007)

Skyora, I new the answer but confused with ur interpreatation of 0123 as 64+16+3
Whats the trick behind this.


----------



## aditya.shevade (Apr 2, 2007)

It's 123 in octal. Means 
	
	



```
(3 * (8**0)) + (2 * (8**1)) + (1 * (8**2))
```

resulting in 
	
	



```
3 + 16 + 64
```

Same as we do in decimals, 123 is 
	
	



```
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 
	
	



```
b ie 11 * (16**0) + a ie 10 * (16**1)
```

equal to 
	
	



```
11 + 160 = 171
```
 in decimal system.

Aditya


----------



## casanova (Apr 2, 2007)

Thanks Aditya for the breakdown


----------



## rahul_becks23 (Apr 3, 2007)

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


----------



## Pathik (Apr 3, 2007)

@sykora thanx man.. Didnt know that..


----------



## aditya.shevade (Apr 3, 2007)

casanova said:
			
		

> Thanks Aditya for the breakdown



Welcome...

Now the next question... what to do if you want to convert the number back to decimal or hex, bin anything?


----------



## Sykora (Apr 3, 2007)

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.


----------



## aditya.shevade (Apr 3, 2007)

^^ 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


----------



## Sykora (Apr 3, 2007)

There are no inbuilt functions afaik. Only third party.


----------



## sivarap (Apr 3, 2007)

Thanks for reminding me of the basics guys.....excellent thread(repped the thread [] )


----------



## abhi_10_20 (Apr 3, 2007)

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????


----------



## aditya.shevade (Apr 4, 2007)

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 (Apr 4, 2007)

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.......


----------



## aditya.shevade (Apr 4, 2007)

^^It results in 0 whenever the shift count is negative, or, while right shifting, if it is going negative. I still have not found the reason, I mean, during right shift, it can go below 0 as 0.5-0.25-0.125......

Aditya


----------

