I was assigned a small C program to implement in a combinational circuit. I've done almost all of the program using a few adders and a multiplexer, but not sure how to complete the last line of the code: n = n % 10;
I figured dividing by 10 would be the first step. However, I have no idea how to divide by 10 - neither do I have any clue on how to get the remainder. I can't shift the bits to the right because that'll just do 1/2^b, and 2^b will never give me 10.
Any ideas?
17 Replies - 2003 Views - Last Post: 06 April 2012 - 07:11 AM
Topic Sponsor:
Replies To: Dividing by 10 in digital logic (and modulo)
#2
Re: Dividing by 10 in digital logic (and modulo)
Posted 10 November 2011 - 08:57 PM
Can you subtract 10 from n?
#3
Re: Dividing by 10 in digital logic (and modulo)
Posted 10 November 2011 - 08:59 PM
Yes. How would that help me?
#4
Re: Dividing by 10 in digital logic (and modulo)
Posted 10 November 2011 - 09:01 PM
What happens if you repeatedly reduce n by 10?
#5
Re: Dividing by 10 in digital logic (and modulo)
Posted 10 November 2011 - 11:29 PM
Ah, I see your point. So to do, say 56%10, I'd do 56-10-10-10 until I get 6. So I need to perform a loop until the answer is negative?
#6
Re: Dividing by 10 in digital logic (and modulo)
Posted 11 November 2011 - 05:09 AM
Just until the answer is less than 10, surely.
#7
Re: Dividing by 10 in digital logic (and modulo)
Posted 16 November 2011 - 08:36 PM
#8
Re: Dividing by 10 in digital logic (and modulo)
Posted 06 December 2011 - 06:16 AM
Just curious if n can be a negative number in the program? If so, the 10 loop still works but you'll need to do some modification to properly handle negative numbers.
#9
Re: Dividing by 10 in digital logic (and modulo)
Posted 05 April 2012 - 03:00 AM
To be more exact, the computer world has confused modulo and reminder.
The modulo-operation should always lead back to the elements of the set, whereas reminder is "leftover" of integral division.
See here.
It's easier to see what to do when you first decide if you want modulo or reminder.
The modulo-operation should always lead back to the elements of the set, whereas reminder is "leftover" of integral division.
See here.
It's easier to see what to do when you first decide if you want modulo or reminder.
#10
Re: Dividing by 10 in digital logic (and modulo)
Posted 05 April 2012 - 11:06 PM
Not sure exactly what you mean by "lead back to the elements of the set" -- what "set" are you referring to?
One of the things with the modulo operator in programming languages versus modular arithmetic in mathematics is that they aren't necessarily the same. Even the wikipedia article you linked to addresses this (in the Remainders section):
Personally, I've had issues with this concept until recently. I fall back to my understanding from mathematics first, but that fails with '%'. I'm afraid "modulo" might not have been the best term for "%", and the result can be inconsistent across programming languages when dealing with negative values.
One of the things with the modulo operator in programming languages versus modular arithmetic in mathematics is that they aren't necessarily the same. Even the wikipedia article you linked to addresses this (in the Remainders section):
Quote
The notion of modular arithmetic is related to that of the remainder in division. The operation of finding the remainder is sometimes referred to as the modulo operation...
In computer science, it is the remainder operator that is usually indicated by either "%" or "mod"...
In computer science, it is the remainder operator that is usually indicated by either "%" or "mod"...
Personally, I've had issues with this concept until recently. I fall back to my understanding from mathematics first, but that fails with '%'. I'm afraid "modulo" might not have been the best term for "%", and the result can be inconsistent across programming languages when dealing with negative values.
#11
Re: Dividing by 10 in digital logic (and modulo)
Posted 05 April 2012 - 11:19 PM
elgose, on 06 April 2012 - 01:06 AM, said:
I'm afraid "modulo" might not have been the best term for "%", and the result can be inconsistent across programming languages when dealing with negative values.
In practical terms, I've never seen this (mod of a negative) cause a problem. That is, I've never come across anyone accidentally taking the mod (yes, that's how it's often called) of a negative number. I could imagine that this would cause a weird "bug" if it ever happened, but I've never come across it. Generally, I think anyone who's going to apply this operator to a negative number is going to know be aware that it might be an issue, and will check the specs to make sure the % operator does what they think it does in that case.
Have you ever come across a situation where this might be an issue, or is this more of a potential problem, never realized?
The only case I can come up with off the top of my head where a programmer might inadvertantly back their way into this error would be a graphing application,if they were doing some calculating on the negative quadrants. I'm sure there are others, but they're not coming to mind.
#12
Re: Dividing by 10 in digital logic (and modulo)
Posted 05 April 2012 - 11:42 PM
It's hardly ever an issue which is probably a primary reason why it's so easy to ignore the difference between modulo and remainder calculation.
On the practical side of things: who cares? You're right in that there is hardly any real-word examples of negative values becoming an issue (at least none I can think of presently). But since we were discussing a matter of theory, I think it's relevant to note that the modulo operator is often a remainder operator (depending on the language) when you have negative values.
Afterall, the OP's questions was surrounding ways to handle n = n % 10;, and who's to say n was ever qualified as non-negative?
On the practical side of things: who cares? You're right in that there is hardly any real-word examples of negative values becoming an issue (at least none I can think of presently). But since we were discussing a matter of theory, I think it's relevant to note that the modulo operator is often a remainder operator (depending on the language) when you have negative values.
Afterall, the OP's questions was surrounding ways to handle n = n % 10;, and who's to say n was ever qualified as non-negative?
#13
Re: Dividing by 10 in digital logic (and modulo)
Posted 05 April 2012 - 11:55 PM
Oh, I agree entirely that it's worth getting down into the details. It just struck me that I simply couldn't think of a case where it could ever come up as an issue. It's one of the most purely theoretical potential issues in language design that I've come across.
#14
Re: Dividing by 10 in digital logic (and modulo)
Posted 06 April 2012 - 12:00 AM
#15
Re: Dividing by 10 in digital logic (and modulo)
Posted 06 April 2012 - 12:02 AM
Well, it was worth writing then.
I was tempted to compare it to similarly theoretical issues... imagine that there was a bug in the implementation of the shift operators in Java. Who would ever notice?
I was tempted to compare it to similarly theoretical issues... imagine that there was a bug in the implementation of the shift operators in Java. Who would ever notice?
|
|

New Topic/Question
Reply



MultiQuote






|