Please explain this statement of mips? - mips

Can anyone explain this to me?
The first line i don't get. How does the first line make the content in $s3 *4?
i know it shifts it to the left how does shifting $s3 by 2 to the left make it 4 times bigger?

Consider decimals. E.g. you shift 12310 2 positions left. You get two zeroes on the right: 1230010. 1 position is equivalent to 1010. 2 positions are equivalent to 10010.
Same with binary numbers. Shift 1012 2 positions left. You get two zeroes on the right: 101002. 1 position is equivalent to 102, which is 210. 2 positions are equivalent to 1002, which is 410.

Related

Predicting Cellular Automata

I am attempting to complete an assignment for an AI course however I cannot understand a question. Unfortunately, I cannot find any information on the internet that clearly explains how to predict the next generation in a CA. I have posted a link to a screenshot of my question below.
Image
Edit:
This is my edited answer;
Edited Answer
In Margolus neighborhoods, the grid is divided into 2x2 blocks. Depending on which step you are in, the division of blocks either starts from the top-left corner or is offset one cell down and one cell to the right. (See Wikipedia on Block cellular automata.) Your instructions say to start from the top-left corner.
So you need to divide up the grid into 2x2 blocks. Then, you check how the patterns in each block match the 15 possible Margolus neighborhood configurations:
For the given grid, you end up with the following. The "neighborhoods" are labeled in yellow highlighted text:
Now you look at the rules you were given: MS, D 0; 14; 11; 5; etc. These numbers after the D tell you, in order, how each configuration should change.
0th number in rule (D 0): Counting from 0, the first number tells you how the 0 (empty) configuration should change. The given number is 0, which means empty 2x2 blocks will not change in the next generation.
1st number in rule (D 0; 14;): The next number tells you how the 1 configuration (one X in upper left corner) should change. That number is 14, which means if we have any 2x2 blocks with the 1 configuration, it should morph into the 14 block. We don't have any 1 configurations, so we go to the next number in the rule.
2nd number in rule (D 0; 14; 11;): The next number tells you how the 2 configuration should change, and that number is 11. We have 2 blocks with the 2 configuration (one X in upper right corner), and the rule tells us we need to convert them to configuration 11 (2x2 block filled with X's except lower left corner).
After evaluating these first 3 rules, you end up with:
Continue for the rest of the numbers in the rule and you will have your answer. As for whether the rule is reversible, see here.

how to right pad a unicode string with space and display it to a fixed length?

OK title can't explain it well.
I need a few lines of unicode strings, which are of the same fixed length. For example,
#1 aabb 999
#2 とうきょう 120
#3 good morning f 60
#4 Привет 51
#5 ...
I can divide each row of the texts into 3 parts. Here the hash with a number means the rank, the string means the name, and the numbers at the end means the score. Note that the third row's name is "good morning friend" but the line is trimmed to "good morning f".
So I have problem padding the name and the score into the correct format, which aligns the row both to the left and to the right.
I am using a div to hold these texts.Currently I am using a span for each line. But I don't know how to pad the strings correctly for different kinds of characters. Anyone has a better idea?
UPDATED:
I managed to do it with a <table> tag. It looks very nice.

Why is over flow occuring in this example?

This is the statement I read online.
If x and y have opposite signs (one is negative, the other is
non-negative), then the sum will never overflow.
But when adding -6 (Binary after two's compliment: 11010) and +9 (Binary : 01001), the answer is overflow: 100011. Which means I was adding two numbers in 5 bits but answer is in 6 bits.
According to the above statement the two numbers with opposite signs will not overflow so then why is this answer an overflow?
Edit: Another question that comes to my mind is that if i am using 5-bits to add -6 and +9 in binary, and the answer I am getting is in 6-bits, then is that an overflow? How can it be detected?

Fast way to evaluate the result of a shift

If I want to see in fast what is the result of a code that does shifting (left/right) I usually write down the binary representation and do the shifting.
But for e.g. shifts of 4 it is actually faster to do it write the hex representation and move the character/digit 1 place to the left/right?
Are there any other tricks for this?
Essentially, shifting 4 bits is removing 1 hex because each hex digit is 4 bits in binary. So shifting 8 bits would be like removing 2 hex, and so on.
If you wanted, you could also do the same type of shift with octal, although instead of 4 bits we would be using 3.
Alternately, if you wish to see the translation in decimal rather than octal or hex, you can view shifting as a way to represent division and multiplication.
With shifting left, you can use x1 << x2 as a form of multiplication by 2^x2.
With shifting right, you can use x1 >> x2 as a form of division by 2^x2. Keep note, this will work for positive numbers, not negative.

Can someone explain to me why overflow always occurs when the last and second last carry bit are different?

overflow = c,n ⊕ c,n−1
I tried it with all four possible cases
c,n c,n-1
-7+2 1001+0010 0 0
7+2 0111+0010 0 1
-7+(-2) 1001+1110 1 0
7+(-2) 0111+1110 1 1
and it seems to work, but can someone explain or prove why?
When adding two numbers with n bits, the result can have n+1 bits. This means that by using n bits for result we cannot represent all of them.
Now, what is an overflow? If we consider a signed number, it will mean that we can extend the number by adding sign bit (MSB) above MSB, and they will be equal. So, when this does not hold (next bit after MSB is not equal to MSB, something that can be detected before truncating the result to n bits), then we say it's overflow.
As is your example, we say that the overflow occurs when after adding two positive number we obtained a negative one (or two negative that gave positive result).
Also, check this answer by Thomas Pornin, I think he explained it wery well.
It's been so long, I have only a vague understanding of what the notation is saying. I'd assume that 'c' is a carry flag, and 'n' is a negative flag. But then what is 'n-1'?
Anyway, I'm guessing your answer pertains to overflow occurring in either direction: from a negative number wrapping over into a positive, and a positive wrapping over into a negative.