English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

5 answers

Easy but pointless, therefore this must be a homework question! nevertheless here goes.. Use the XOR and SHIFT operators. Also, it is assumed that when using the XOR operator that the result is placed in the first operand, i.e, b=b XOR c

Example pseudo code adding 00000001 (1) to 10110011(179).

Variables:

a=10110011 ( Original number and will also contain final result )
b ( temp variable )
c ( temp variable )

Routine:

1) b=a
2) c=00000001
3) b XOR c
4) IF b>a, THEN a=b, EXIT
5) a=b
6) SHIFTLEFT c
7) GOTO 3)

Basically all that is happening is that working from right to left, you are removing any '1's from the original number until you find a '0' which you then change to a '1'.
NOTE: you will have to check that you are looping no more than the number of bits in the number otherwise if a==11111111, it will be reset to zero and you will end up in an infinite loop! ;o)

2006-08-30 03:52:00 · answer #1 · answered by Sal 2 · 0 0

You don't. The increment operator is used to increment a number in c++.

Bitwise operators are used for bitwise operations.

2006-08-30 03:11:48 · answer #2 · answered by Anonymous · 0 0

commonly speaking that's key to dodge branch as this consumes maximum CPU of all base operations. maximum preferrable are addition and subtraction, multiplication is next maximum proper. Modulo: ====== the authentic result for the modulo operation relies upon on the smallest digit in worry-free words, e.g. a million % 10 has similar effect as 100'001 % 10 has similar effect as 999999999999991 % 10 So modulo should be simulated through m = 0; for(int i = 0; i < len; i++){ d[i] = m; m = m+a million; if(m>9) then { m = 0; } } even if it really is faster than your code desires to be confirmed, yet i'm confident it really is, and extensively so. Integer branch ================ as an party, 100 via 109 have a similar integer branch effect, quite, 10. this would nicely be used to simplify as follows: for(int i = 0; i < len; i=i+10) // note the addition of ten at a time { num[i] = arr[i] / 10; for(int j = a million; j < 10; j++) // j = 0 has been coated already { num[i+j] = num[i]; } } Seeing the above code for the dep. you probable note a trend, as what i did for the integer branch should be repeated all yet another time. it really is many times an indication to apply recursion, and in reality i imagine recustion will be ideal the following (it would also in tremendous condition the modulo trend). yet which will be previous the scope of this answer so I depart this to you.

2016-10-15 22:10:19 · answer #3 · answered by Anonymous · 0 0

This sounds like a homework problem - as noone would want to do this otherwise.

You will have to implement a 2 bit addition function with carry and then repeat and shift for 32 bits, if you are using 32-bit integers.

2006-08-30 02:10:24 · answer #4 · answered by rscanner 6 · 0 0

seems like a pretty silly task. the compiler optimizes the increment operator.

you use a barrel shifter i suppose. you would only need to use the '<<' and '&'.

2006-08-30 02:40:22 · answer #5 · answered by cw 3 · 0 0

fedest.com, questions and answers