Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-04-2007, 11:18 AM   #1 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Exclamation In C, why is 123 - 0123 != 0????


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
__________________
--- Console Junkie
aditya.shevade is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 02-04-2007, 05:03 PM   #2 (permalink)
Alpha Geek
 
rahul_becks23's Avatar
 
Join Date: Oct 2004
Posts: 832
Default Re: In C, why is 123 - 0123 != 0????

what do u initialize the values with ....... an int , double .....etc ..... which one ???
rahul_becks23 is offline  
Old 02-04-2007, 05:12 PM   #3 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: In C, why is 123 - 0123 != 0????

^^ I tried
Code:
int x=0123;
then
Code:
const int x=0123;
__________________
--- Console Junkie
aditya.shevade is offline  
Old 02-04-2007, 05:52 PM   #4 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: In C, why is 123 - 0123 != 0????

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.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 02-04-2007, 06:23 PM   #5 (permalink)
Apprentice
 
subhajitmaji's Avatar
 
Join Date: Oct 2006
Posts: 55
Default Re: In C, why is 123 - 0123 != 0????

Quote:
Originally Posted by Sykora
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
__________________
Driving on the highway is not a competition. It is a co-operation, the sharing of a limited resource.
subhajitmaji is offline  
Old 02-04-2007, 09:28 PM   #6 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: In C, why is 123 - 0123 != 0????

Thank you very much Sykora.... no reps now, or you would have received one
__________________
--- Console Junkie
aditya.shevade is offline  
Old 02-04-2007, 09:35 PM   #7 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: In C, why is 123 - 0123 != 0????

It's the thought that counts.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 02-04-2007, 09:44 PM   #8 (permalink)
The Frozen Nova
 
casanova's Avatar
 
Join Date: Sep 2004
Location: Trespasser in Virtual Land
Posts: 1,641
Default Re: In C, why is 123 - 0123 != 0????

Skyora, I new the answer but confused with ur interpreatation of 0123 as 64+16+3
Whats the trick behind this.
__________________
I dream of a better tomorrow... where chickens can cross roads and not have their motives questioned.

www.nerdweed.blogspot.com
casanova is offline  
Old 02-04-2007, 10:19 PM   #9 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: In C, why is 123 - 0123 != 0????

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
__________________
--- Console Junkie
aditya.shevade is offline  
Old 02-04-2007, 10:52 PM   #10 (permalink)
The Frozen Nova
 
casanova's Avatar
 
Join Date: Sep 2004
Location: Trespasser in Virtual Land
Posts: 1,641
Default Re: In C, why is 123 - 0123 != 0????

Thanks Aditya for the breakdown
__________________
I dream of a better tomorrow... where chickens can cross roads and not have their motives questioned.

www.nerdweed.blogspot.com
casanova is offline  
Old 03-04-2007, 12:48 AM   #11 (permalink)
Alpha Geek
 
rahul_becks23's Avatar
 
Join Date: Oct 2004
Posts: 832
Default Re: In C, why is 123 - 0123 != 0????

thanks for the info .......... but i guess i'll have to read abt that somewhere in more detail .
rahul_becks23 is offline  
Old 03-04-2007, 12:59 AM   #12 (permalink)
Google Bot
 
Pathik's Avatar
 
Join Date: Aug 2005
Posts: 9,772
Default Re: In C, why is 123 - 0123 != 0????

@sykora thanx man.. Didnt know that..
Pathik is offline  
Old 03-04-2007, 02:02 AM   #13 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: In C, why is 123 - 0123 != 0????

Quote:
Originally Posted by casanova
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?
__________________
--- Console Junkie
aditya.shevade is offline  
Old 03-04-2007, 08:43 AM   #14 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: In C, why is 123 - 0123 != 0????

Quote:
Originally Posted by aditya.shevade
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.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 03-04-2007, 12:26 PM   #15 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: In C, why is 123 - 0123 != 0????

^^ 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
__________________
--- Console Junkie
aditya.shevade is offline  
Old 03-04-2007, 01:20 PM   #16 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: In C, why is 123 - 0123 != 0????

There are no inbuilt functions afaik. Only third party.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 03-04-2007, 03:19 PM   #17 (permalink)
In The Zone
 
sivarap's Avatar
 
Join Date: Mar 2007
Posts: 340
Default Re: In C, why is 123 - 0123 != 0????

Thanks for reminding me of the basics guys.....excellent thread(repped the thread [] )
sivarap is offline  
Old 03-04-2007, 06:06 PM   #18 (permalink)
Cool and Calm
 
abhi_10_20's Avatar
 
Join Date: Jul 2006
Location: Bangalore
Posts: 406
Default Re: In C, why is 123 - 0123 != 0????

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????
__________________
When Roger's at play, opponents pray..!!!!! :twisted:
abhi_10_20 is offline  
Old 04-04-2007, 12:49 AM   #19 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: In C, why is 123 - 0123 != 0????

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
__________________
--- Console Junkie
aditya.shevade is offline  
Old 04-04-2007, 01:42 PM   #20 (permalink)
Cool and Calm
 
abhi_10_20's Avatar
 
Join Date: Jul 2006
Location: Bangalore
Posts: 406
Default Re: In C, why is 123 - 0123 != 0????

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.......
__________________
When Roger's at play, opponents pray..!!!!! :twisted:
abhi_10_20 is offline  
Old 04-04-2007, 05:43 PM   #21 (permalink)
Console Junkie
 
aditya.shevade's Avatar
 
Join Date: Jun 2006
Location: USA
Posts: 991
Default Re: In C, why is 123 - 0123 != 0????

^^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
__________________
--- Console Junkie
aditya.shevade is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Sujeet
- by Krow
- by abhidev

Advertisement




All times are GMT +5.5. The time now is 07:56 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2