You are on page 1of 3

20/08/2013

c - Subtracting two numbers without using '-' operator - Stack Overflow

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Tell me more

×

Subtracting two numbers without using '-' operator

i tried with the following code , but i can't understand why it's giving me wrong answer. i am computing the 2's complement and adding with another no. # i n c l u d e< s t d i o . h > i n ta d d ( i n ta ,i n tb ){ w h i l e( a ){ a=( a&b )< <1 ; b=a ^ b ; } r e t u r nb ; } i n ts u b ( i n ta ,i n tb )/ /a d daw i t hb ' s2 ' sc o m p l e m e n t . { r e t u r n( a d d ( a ,a d d ( ~ b ,1 ) ) ) ; } i n tm a i n ( ){ i n ta ,b ,r e s ; a=3 ,b=1 ; r e s=s u b ( a ,b ) ; p r i n t f ( " % d \ n " ,r e s ) ; r e t u r n0 ; }
c bitwise

edited Aug 8 '10 at 13:54 Carlos Muñoz 4,699 2 20 46

asked Aug 7 '10 at 13:45 pranay 619 7 26

3

s u b ( ) is giving you the wrong result because a d d ( ) is wrong. The logic in s u b ( ) is fine. – NullUserException ♦ Aug 7 '10 at 13:49 What's wrong with -? What's wrong with a + b? – Charles Bailey Aug 7 '10 at 13:51 This brings back memories too. Our professor wouldn't let us use loops, or i f statements for that matter. – NullUserException ♦ Aug 7 '10 at 13:51

1

@Charles It's homework. – NullUserException ♦ Aug 7 '10 at 13:51 @NullUserException: So what if it's homework; it's also tagged as C. + and - are always available in C. This feels very much like "not a real question" to me. – Charles Bailey Aug 7 '10 at 14:04

show 3 more comments

3 Answers
Here is better solution http://stackoverflow.com/questions/1149929/ # i n c l u d e< s t d l i b . h >/ *a t o i ( )* / # i n c l u d e< s t d i o . h > / *( f ) p r i n t f* / # i n c l u d e< a s s e r t . h >/ *a s s e r t ( )* / i n ta d d ( i n tx ,i n ty ){ i n tc a r r y=0 ; i n tr e s u l t=0 ; i n ti ;

stackoverflow.com/questions/3430651/subtracting-two-numbers-without-using-operator

1/3

i< =t o . a s s e r t ( i*j= =m u l t i p l y ( i . The } following is an odd way to do it (just since other better ways are already given) stackoverflow. it works now: i used a different add() function x=m u l t i p l y _ b y _ t w o ( x ) . following will compute a . } x = a ^ b .1 ) .+ + j ){ Considering how negative numbers are represented.+ + i ){ i n ta=( x> >i )&1 . – Flexo ♦ Mar 10 at 10:03 } a s s e r t ( i/2= =d i v i d e _ b y _ t w o ( i ) ) .n e g a t e ( y ) ) . r e s u l t| =( ( a^b )^c a r r y )< <i .n e g a t e ( y ) ) . pranay s s e r t ( ( ( i%2 )= =0 )= =i s _ e v e n ( i ) ) .+ + i ){ answered Aug 7f '10 at 14:15 a s s e r t ( 0-i= =n e g a t e ( i ) ) .com/questions/1149929/… please vote for it there – Dan D. that is of course wrong. 619 a 7 26 a s s e r t ( i*2= =m u l t i p l y _ b y _ t w o ( i ) ) .b: a s s e r t ( i-j= =s u b t r a c t ( i .20/08/2013 c .Stack Overflow f o r ( i=0 . / / b = ( a ^ b ) .j< =t o . noted:) This moves the attention to your add implementation. i n tm a i n ( i n ta r g c . thanks but i was trying to use the add funtion mentioned in a post below it by Tom Leys . f ( i s _ e v e n ( i ) ) while(a&b) { This willi through an error on statement as a and b is not boolean – Chirag Tayal Feb 24 at 4:47 1 @ChiragTayal in C almost anything can be a boolean.+ + i ){ f o r ( j=f r o m ./ /( )n o tn e e d e d .1 ) .Subtracting two numbers without using '-' operator . c a r r y=( a&b )|( b&c a r r y )|( c a r r y&a ) . y d i v i d e _ b y _ t w o ( y ) . i n t14:12 y ){ working –t pranay Aug 7 '10 at i n tr e s u l t=0 . } r e t u r nr e s u l t . i n ta d d ( i n ta . a = x .com/questions/3430651/subtracting-two-numbers-without-using-operator 2/3 .i n ty ){ r e t u r na d d ( x . r e t u r n x . 83. } i n ts u b t r a c t ( i n tx . n ti . } w h i l e ( a & b ) { } b = ( ( a & b ) < < 1 ) .j ) ) . i n tx .5k 10 96 162 } i n tm u l i p l y ( i n tx .j u s tt os h o wt h ep o i n t }+( as ther OP already e t u r n0 . } i f o r ( i= r o m . donno why it's not this is from stackoverflow. } answered Aug 7 '10 at 13:49 i n tm u l t i p l y _ b y _ t w o ( i n tn ){ Jigar Joshi r e t u r n n< <1 . y=a d d ( y . i n t= b ) }e l s e{ { r e s u l t=a d d ( r e s u l t . the a s s e r t ( i+j = = a d d ( i . } f o r ( i=f r o m . r e t u r nr e s u l t .t o=1 0 0 .j ) ) . i n ta . i n tb=( y> >i )&1 .b .x ) . i< =t o . } i n tn e g a t e ( i n tx ){ r e t u r na d d ( ~ x . } i n ti s _ e v e n ( i n tn ){ r e t u r n! ( n&1 ) .x ) . } w h i l e ( y>0 ){ i f ( i s _ e v e n ( y ) ) {as suggested by NullUserException.j ) ) .c .i<3 2 . x = a ^ b . Aug 7 '10 at 14:30 i f ( x<0& &y<0 ){ r e t u r nm u l t i p l y ( n e g a t e ( x ) . / /a s s i g n oaa n db }t c=a ~ b+1 ) .j .c h a r* * a r g v ){ } i n t f r o m=1 0 0 . } i f ( x> =0& &y<0 ){ r e t u r nm u l t i p l y ( y . } i n td i v i d e _ b y _ t w o ( i n tn ){ r e t u r nn> >1 .

and it is obscure the connection between title and code. } (keeping the same idea the code can be made better.i<s i z e o f ( i n t ) * 8 . } i n ti n v ( i n ta ) { i n ti . f o r ( i=0 . f o r ( i = 0 .& c ) .i n tb ) { r e t u r na d d ( a . * c=a & b|a & r|b & r . Maybe he wants to be sure you understood 2's complement.all that code just hides that! .i n t* c ) { i n tr=* c&1 .b& =1 .titles should be more "catch it" – ShinTakezou Aug 7 '10 at 16:25 Not the answer you're looking for? Browse other questions tagged c bitwise or ask your own question. r< < =1 . This shows the solution is right. just too tired to do it finer) edited Aug 7 '10 at 17:46 answered Aug 7 '10 at 16:18 ShinTakezou 4.718 7 15 The OP got that. so the answer can't be "unuseful" (as the down button say) – ShinTakezou Aug 7 '10 at 16:24 ops .1 ) ) .r=0 . that is just the way you thought it could be done. } r e t u r nr < < 1 . and ~ being a bitwise operator. Moreover. r e t u r na ^ b ^ r .i + + ) { r| =a d d 1 ( a > > i . i n tc=0 .so this too is taken into account. stackoverflow.20/08/2013 c . a& =1 . – quantumSoup Aug 7 '10 at 16:20 the title being "Subtracting two numbers without using ‘-’ operator" which does not mean implement add/sub bitwise like your shown code. } i n ts u b ( i n ta .com/questions/3430651/subtracting-two-numbers-without-using-operator 3/3 .i n tb .a d d ( ~ b .i<s i z e o f ( i n t ) * 8 . here there's not this simple insight.Stack Overflow i n ta d d 1 ( i n ta . makes the whole code bitwise .i . } r e t u r ni n v ( r ) . } i n ta d d ( i n ta .Subtracting two numbers without using '-' operator .b > > i . but there's no clue about why teacher asked you that. provided that add is implemented correctly :) .i n tb ) { i n tr=0 . a> > =1 .i + + ) { r=r < < 1|( a & 1 ) .