How can i make:
x % 10
x / 10
with bitwise & and bitshifting?
Thank you!
Asked
Active
Viewed 98 times
0
-
2Have you done % 8 and / 8? That's quite simple. A number that isn't a power of 2 is much harder (not sure it's possible with just those two operators - I think you need NAND). – Bathsheba Mar 28 '18 at 15:48
-
1Why would you want to? – Eugene Sh. Mar 28 '18 at 15:48
-
I have to translate my C-Programm into assembler language. And now i would change this, because it is simpler to translate this. – Abc Mar 28 '18 at 15:55
-
Why don't you compile a program that uses those operations and look at the assembly code produced to see how it is done? – Christian Gibbons Mar 28 '18 at 15:57
-
1Which assembler? Doesn't it have an integer division instruction? – Eugene Sh. Mar 28 '18 at 15:57
-
With some extra addition/subtraction it works quite well but only bitwise "and" and bit-shift? What about a "not"? Can we have one? Because a lot is possible with just a handful of "nand" gates! – deamentiaemundi Mar 28 '18 at 16:02
-
Related: https://stackoverflow.com/questions/5558492/divide-by-10-using-bit-shifts – David Ranieri Mar 28 '18 at 16:03
-
No. That is why i want to change this. – Abc Mar 28 '18 at 16:03
-
i have bitwise "and","xor","left shift" and "right shift" – Abc Mar 28 '18 at 16:04
-
and i have ADD and SUB – Abc Mar 28 '18 at 16:04
-
@KeineLust Looks like duplicate... – Eugene Sh. Mar 28 '18 at 16:06
-
@Abc in that case let me point you to https://stackoverflow.com/questions/5558492/divide-by-10-using-bit-shifts where your question gets answered. – deamentiaemundi Mar 28 '18 at 16:07
-
@EugeneSh. I don't want to close it as duplicate because OP is also asking for MOD 10 and the link is not answering this point. – David Ranieri Mar 28 '18 at 16:07
-
does somebody has any ideas? – Abc Mar 28 '18 at 16:12
-
You've got the link to the answer. Twice! – Eugene Sh. Mar 28 '18 at 16:12
-
@KeineLust I will then. `x % 10 = x - (x / 10) * 10`. – Eugene Sh. Mar 28 '18 at 16:13
-
But not `mod`, sorry! To get the remainder you do `mod = x - ((x / z) * z)`. Multiplication with 10 can be done in that way: `y = (x << 3) + (x << 1)` – deamentiaemundi Mar 28 '18 at 16:14
-
@EugeneSh. Sure! But I think OP wants to do this using bitwise ops. – David Ranieri Mar 28 '18 at 16:14