Sunteți pe pagina 1din 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 Muoz 4,699 2 20 46

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

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

@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

20/08/2013

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


f o r ( i=0 ;i<3 2 ;+ + i ){ i n ta=( x> >i )&1 ; i n tb=( y> >i )&1 ; r e s u l t| =( ( a^b )^c a r r y )< <i ; c a r r y=( a&b )|( b&c a r r y )|( c a r r y&a ) ; } r e t u r nr e s u l t ; } i n tn e g a t e ( i n tx ){ r e t u r na d d ( ~ x ,1 ) ; } i n ts u b t r a c t ( i n tx ,i n ty ){ r e t u r na d d ( x ,n e g a t e ( y ) ) ; } i n ti s _ e v e n ( i n tn ){ r e t u r n! ( n&1 ) ; } i n td i v i d e _ b y _ t w o ( i n tn ){ r e t u r nn> >1 ; }
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 ; 83.5k 10 96 162

i n tm u l i p l y ( i n tx , i n t14:12 y ){ working t pranay Aug 7 '10 at i n tr e s u l t=0 ;

thanks but i was trying to use the add funtion mentioned in a post below it by Tom Leys , donno why it's not this is from stackoverflow.com/questions/1149929/ please vote for it there Dan D. 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 ) ,n e g a t e ( y ) ) ; } i f ( x> =0& &y<0 ){ r e t u r nm u l t i p l y ( y ,x ) ; } w h i l e ( y>0 ){ i f ( i s _ e v e n ( y ) ) {as suggested by NullUserException, it works now: i used a different add() function x=m u l t i p l y _ b y _ t w o ( x ) ; y d i v i d e _ b y _ t w o ( y ) ; i n ta d d ( i n ta , i n t= b ) }e l s e{ { r e s u l t=a d d ( r e s u l t ,x ) ; i n tx ; y=a d d ( y ,1 ) ; x = a ^ b ; } w h i l e ( a & b ) { } b = ( ( a & b ) < < 1 ) ; r e t u r nr e s u l t ; a = x ; } x = a ^ b ; / / b = ( a ^ b ) ; i n tm a i n ( i n ta r g c ,c h a r* * a r g v ){ } i n t f r o m=1 0 0 ,t o=1 0 0 ; r e t u r n x ; n ti ,j ; } i f o r ( i= r o m ; i< =t o ;+ + i ){ answered Aug 7f '10 at 14:15 a s s e r t ( 0-i= =n e g a t e ( i ) ) ; pranay s s e r t ( ( ( i%2 )= =0 )= =i s _ e v e n ( i ) ) ; 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 ) ) ; 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. 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 ) ) ;

} f o r ( i=f r o m ;i< =t o ;+ + i ){ f o r ( j=f r o m ;j< =t o ;+ + j ){ Considering how negative numbers are represented, the a s s e r t ( i+j = = a d d ( i ,j ) ) ; following will compute a - b: a s s e r t ( i-j= =s u b t r a c t ( i ,j ) ) ; i n ta ,b ,c ; a s s e r t ( i*j= =m u l t i p l y ( i ,j ) ) ; / /a s s i g n oaa n db }t c=a ~ b+1 ) ;/ /( )n o tn e e d e d ,j u s tt os h o wt h ep o i n t }+( as ther OP already e t u r n0 ; noted:) This moves the attention to your add implementation, that is of course wrong. The } following is an odd way to do it (just since other better ways are already given)

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

2/3

20/08/2013

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

i n ta d d 1 ( i n ta ,i n tb ,i n t* c ) { i n tr=* c&1 ; a& =1 ;b& =1 ; * c=a & b|a & r|b & r ; r e t u r na ^ b ^ r ; } i n ti n v ( i n ta ) { i n ti ,r=0 ; f o r ( i=0 ;i<s i z e o f ( i n t ) * 8 ;i + + ) { r=r < < 1|( a & 1 ) ; a> > =1 ; } r e t u r nr < < 1 ; } i n ta d d ( i n ta ,i n tb ) { i n tr=0 ,i ; i n tc=0 ; f o r ( i = 0 ;i<s i z e o f ( i n t ) * 8 ;i + + ) { r| =a d d 1 ( a > > i ,b > > i ,& c ) ; r< < =1 ; } r e t u r ni n v ( r ) ; } i n ts u b ( i n ta ,i n tb ) { r e t u r na d d ( a ,a d d ( ~ b ,1 ) ) ; } (keeping the same idea the code can be made better, 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. 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, that is just the way you thought it could be done, but there's no clue about why teacher asked you that. Maybe he wants to be sure you understood 2's complement, and ~ being a bitwise operator, makes the whole code bitwise - so this too is taken into account. Moreover, here there's not this simple insight, so the answer can't be "unuseful" (as the down button say) ShinTakezou Aug 7 '10 at 16:24 ops - all that code just hides that! - and it is obscure the connection between title and code. This shows the solution is right, provided that add is implemented correctly :) - 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.

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

3/3

S-ar putea să vă placă și