02-04-2007, 11:18 AM
|
#1 (permalink)
|
|
Console Junkie
Join Date: Jun 2006
Location: USA
Posts: 991
|
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
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
02-04-2007, 05:03 PM
|
#2 (permalink)
|
|
Alpha Geek
Join Date: Oct 2004
Posts: 832
|
Re: In C, why is 123 - 0123 != 0????
what do u initialize the values with ....... an int , double .....etc ..... which one ???
|
|
|
02-04-2007, 05:12 PM
|
#3 (permalink)
|
|
Console Junkie
Join Date: Jun 2006
Location: USA
Posts: 991
|
Re: In C, why is 123 - 0123 != 0????
__________________
--- Console Junkie
|
|
|
02-04-2007, 05:52 PM
|
#4 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
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 --
|
|
|
02-04-2007, 06:23 PM
|
#5 (permalink)
|
|
Apprentice
Join Date: Oct 2006
Posts: 55
|
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.
|
|
|
02-04-2007, 09:28 PM
|
#6 (permalink)
|
|
Console Junkie
Join Date: Jun 2006
Location: USA
Posts: 991
|
Re: In C, why is 123 - 0123 != 0????
Thank you very much Sykora.... no reps now, or you would have received one
__________________
--- Console Junkie
|
|
|
02-04-2007, 09:35 PM
|
#7 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
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 --
|
|
|
02-04-2007, 09:44 PM
|
#8 (permalink)
|
|
The Frozen Nova
Join Date: Sep 2004
Location: Trespasser in Virtual Land
Posts: 1,641
|
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
|
|
|
02-04-2007, 10:19 PM
|
#9 (permalink)
|
|
Console Junkie
Join Date: Jun 2006
Location: USA
Posts: 991
|
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
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
in decimal system.
Aditya
__________________
--- Console Junkie
|
|
|
02-04-2007, 10:52 PM
|
#10 (permalink)
|
|
The Frozen Nova
Join Date: Sep 2004
Location: Trespasser in Virtual Land
Posts: 1,641
|
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
|
|
|
03-04-2007, 12:48 AM
|
#11 (permalink)
|
|
Alpha Geek
Join Date: Oct 2004
Posts: 832
|
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 .
|
|
|
03-04-2007, 12:59 AM
|
#12 (permalink)
|
|
Google Bot
Join Date: Aug 2005
Posts: 9,772
|
Re: In C, why is 123 - 0123 != 0????
@sykora thanx man.. Didnt know that..
|
|
|
03-04-2007, 02:02 AM
|
#13 (permalink)
|
|
Console Junkie
Join Date: Jun 2006
Location: USA
Posts: 991
|
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
|
|
|
03-04-2007, 08:43 AM
|
#14 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
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 --
|
|
|
03-04-2007, 12:26 PM
|
#15 (permalink)
|
|
Console Junkie
Join Date: Jun 2006
Location: USA
Posts: 991
|
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
|
|
|
03-04-2007, 01:20 PM
|
#16 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
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 --
|
|
|
03-04-2007, 03:19 PM
|
#17 (permalink)
|
|
In The Zone
Join Date: Mar 2007
Posts: 340
|
Re: In C, why is 123 - 0123 != 0????
Thanks for reminding me of the basics guys.....excellent thread(repped the thread [  ] )
|
|
|
03-04-2007, 06:06 PM
|
#18 (permalink)
|
|
Cool and Calm
Join Date: Jul 2006
Location: Bangalore
Posts: 406
|
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:
|
|
|
04-04-2007, 12:49 AM
|
#19 (permalink)
|
|
Console Junkie
Join Date: Jun 2006
Location: USA
Posts: 991
|
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
|
|
|
04-04-2007, 01:42 PM
|
#20 (permalink)
|
|
Cool and Calm
Join Date: Jul 2006
Location: Bangalore
Posts: 406
|
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:
|
|
|
04-04-2007, 05:43 PM
|
#21 (permalink)
|
|
Console Junkie
Join Date: Jun 2006
Location: USA
Posts: 991
|
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
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|