I included a function (a%b + b) % b in some old cold and remember concluding that this was due to some special cases of a%b that I needed to be careful about. a and b are c ints and % is the c modulo operator. Now I am having trouble seeing where these two expressions ever differ. Are they completely equivalent?
The mathematical long division demands that the remainder is zero or positive, a=q*b+r with 0 <= r < b.
In the computer implementations of this operation it is possible that a%b is negative. Thus adding b then gives the non-negative remainder. To be universally useful you either need an if-branching or another remainder operation for the case where a%b already was non-negative.
The % operator does not implement a true modulo. In fact,
a ≥ 0 -> a % b = a mod b
a < 0 -> a % b = - ((-a) mod b)
Now,
a -4 -3 -2 -1 0 1 2 3 4
a mod 4 0 1 2 3 0 1 2 3 0
a % 4 0 -3 -2 -1 0 1 2 3 0
(a % 4 + 4) % b 0 1 2 3 0 1 2 3 0
Unfortunately, this doubles the cost of the modulo, which is significant.
Related
Can anyone please help me convert number (for example) 143,625 to binary? I've been searching through net for quite a long time but didn't find anything with good explanation.
Thanks in advance!
The integer part can be done by dividing by 2 repeatedly and keeping track of the remainder:
143 / 2 = 71 remainder 1
71 / 2 = 35 remainder 1
35 / 2 = 17 remainder 1
17 / 2 = 8 remainder 1
8 / 2 = 4 remainder 0
4 / 2 = 2 remainder 0
2 / 2 = 1 remainder 0
1 / 2 = 0 remainder 1
So the integer part is 10001111
For the fractional part, multiply it by 2 repeatedly and look at the integer part of the result:
.625 x 2 = 1.25 - we need the integer part, which is 1 - so far we have 0.1, we then disregard the 1 and look at 0.25
0.25 x 2 = 0.5 - so far we have 0.10, we look at 0.5
0.5 x 2 = 1.0 - we have 0.101, no decimal part so we're good.
The whole number is the integer part + the decimal part, so 10001111.101
Using 8 bit registers and signed magnitude representation.
I thought 25 in BCD is 010 0101 but my text book says it as 001 1001. Can somebody explain?
25 / 2 = 12r1 (12 with a remainder of 1)
12 / 2 = 6r0 (6 with a remainder of 0)
6 / 2 = 3r0 (3 with a remainder of 0)
3 / 2 = 1r1 (1 with a remainder of 0)
1 / 2 = 0r1 (0 with a remainder of 0)
So 11001 (working backward up the tree) is the binary equivalent to 25.
Another way to think about it is with powers of 2:
(1*16) + (1*8) + (0*4) + (0*2) + (1*1) = 25
And it's worth noting, just as in base 10, leading zeros do not change the value of a number. (00025 == 25) (0011001 == 11001).
The leading zeros are there in your case because your needing to populate an 8 bit register (there needs to be 8 binary digits regardless of their value).
fact :: Int -> Int
fact n
|n < 0 = 0
|n == 0 = 1
|n > 0 && n `mod` 2 == 1 = fact (n-1) * n
|n > 0 && n `mod` 2 == 0 = n-1
When I enter an odd number for example: fact 5 will give 15, as it should 1 * 3 * 5 = 15. However I realized that if I do fact 7 or any other odd number, it only multiplies the first two odd numbers. How do I get the function to multiply all the odd numbers and not just the first 2. Eg. fact 7 = 35 (ie. 3 * 5). Also note, that if an even number is entered, it will work out the factorial of all the odd numbers up until and not including the even number.
This reminds me of the famous Evolution of a Haskell Programmer. Paraphrasing the tenured professor's answer:
factorialOfOdds :: Integer -> Integer
factorialOfOdds n = product [1,3..n]
You're problem is that your case for an even number is n-1, which means that when you get to an odd number you're really just doing
n * (n - 1 - 1)
When what you want is
n * n-2 * n-4 ...
So try this instead
fact :: Integer -> Integer -- Overflows
fact n
|n < 0 = 0
|n == 0 || n == 1 = 1
|n `mod` 2 == 1 = fact (n-2) * n
|n `mod` 2 == 0 = fact (n-1)
I also took the liberty of removing some redundant logic. Here we decrement by two if it's odd, so 5 -> 3. And in the even case we decrement by one to end up on an odd number and that recurse on that.
What are w-bit words in computer architecture ?
For two 7 bit words
1011001 = A
1101011 = B , how does multiplication returns
10010100110011 ?
Isn't there simple binary multiplication involved in these ?
Please provide an example.
w-bit is just the typical nomenclature for n-bit because w is usually short for word size
Both adding and multiplying are done just the same as in decimal (base 10). You just need to remember this truth table:
Multiplying
-----------
0 x 0 = 0
0 x 1 = 0
1 x 0 = 0
1 x 1 = 1
Adding
-----------
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 (w/ carry)
First adding. To add, you add just like you would in normal arithmetic, except follow the truth table above:
00000101 = 5
+ 00000011 = 3
--------------
00001000 = 8
How this works is that you start from the right and work left. 1 + 1 = 0, but you carry a 1 over to the next column. So the next column is 0 + 1, which would be 1, but since you carried another 1 from the previous column, its really 1 + 1, which is 0. You carry a 1 over the next column, which is 1 + 0, but really 1 + 1 because of the carry. So 0 again and finally move the 1 to the next column, which is 0 + 0, but because of our carry, becomes 1 + 0, which is 1. So our answer is 1000, which is 8 in decimal. 5 + 3 = 8, so we know we are right.
Next, multiplying:
00000101 = 5
x 00000011 = 3
----------
101 = 5
+ 1010 = 10
----------
1111 = 15
How this works is you multiply the top number 00000101 by the right most digit in the second row. So 00000011 is our second row and 1 is the right most digit, so 00000101 times 1 = 101. Next you put a 0 placeholder in the right most column below it, just like in normal multiplication. Then you multiply our top original number 00000101 by the next digit going left in our original problem 00000011. Again it produce 101. Next you simply add 101 + 1010 = 1111 ...That is the answer
Yes, it's simple binary multiplication:
>>> 0b1011001
89
>>> chr(_)
'Y'
>>> 0b1101011
107
>>> chr(_)
'k'
>>> ord('Y') * ord('k')
9523
>>> bin(_)
'0b10010100110011'
If you want to multiply, you simply do the multiplication the same as with decimal numbers, except that you have to add the carries in binary:
1011001
x1101011
-------
1011001
1011001.
0000000..
1011001...
0000000....
1011001.....
1011001......
--------------
10010100110011
w-bit words aren't anything by themselves. Assuming that the value of w has been previously defined in the context in which "w-bit word" is used, then it simply means a word that is composed of w bits. For instance:
A version of RC6 is more accurately specified as RC6-w/r/b where the word size
is "w" bits, encryption consists of a nonnegative number of rounds "r," and
"b" denotes the length of the encryption key in bytes. Since the AES
submission is targetted at w=32, and r=20, we shall use RC6 as shorthand to
refers to such versions.
So in the context of that document, a "w-bit word" is just a 32-bit value.
As for your multiplication, I'm not sure what you are asking. Google confirms the result as correct:
1011001 * 1101011 = 10010100110011
What does the equivalence sign with the small 3 next to it mean... I'm unfamiliar with that sign.
≡3
Thanks!
This means "is congruent to modulo 3." For example, 7 ≡3 1 because 7 mod 3 = 1 = 1 mod 3. More formally, a ≡n b iff there exists integral c0, c1, along with an integral k with 0 ≤ k < n, such that a = nc0 + k and b = nc1 + k.