Re: Bitwise operators



On 19 Giu, 11:32, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
den2k said:
Given subtraction, division becomes not only possible but even easy.
Consider 34 / 10 (which we need to do if we're going to get the result 3
and remainder 4). 34 is 100010 in binary, and ten is 1010. Observe long
division in binary, noting that subtraction is done with ^ & << and the
two's complement, comparison can be done by subtraction, isolating the
bits necessary for the trial divisions can be done using shift and AND,
and "bringing down a bit" can be done with shift and OR:

1010)100010
1010 <---- Subtrahend <= minuend? No.

_0000
1010)100010
1010 <---- Subtrahend <= minuend? Yes. Subtract.

_00001
1010)100010
1010
---
111 <----- bring down a bit

_00001
1010)100010
1010
---
1110
1010 <---- Subtrahend <= minuend? Yes. Subtract.

_000011 <---- Result: 3
1010)100010
1010
---
1110
1010
---
100 <---- Remainder: 4

Yes, Virginia, you *can* divide by ten, even if you're thinking in binary.


Whoops I unconsciously thougt to a sigle bitwise operator.. and I
didn't know so well the use of bitwise Operators in order to do the
basilar operations. Thanks a lot.
.



Relevant Pages

  • Re: what should be the output
    ... which is the correct output: ... There's no undefined behavior in this use ... so the result of the subtraction is 0xfffffffdUL. ... so after the shift the result is 0x7ffffffUL. ...
    (comp.lang.c)
  • Re: division by 7
    ... How would you divide a number by 7 without using division operator? ... I did by doing a subtraction and keeping a counter that kept a tab on ... So to divide by 7: shift left 3, then again, then again, etc. ...
    (sci.math)
  • Re: Catching Keyboard buttons (multiple)
    ... The problem is that I'm trying to catch when the ALT and the SHIFT ... or CONTROL key is pressed along with the 'D' key. ... > operator not a bitwise operator. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: division by 7 without using division operator
    ... How would you divide a number by 7 without using division operator? ... I did by doing a subtraction and keeping a counter that kept a tab on ... Since 7 has a binary representation 111, my guess is that a left shift ...
    (comp.lang.c)