Shifting through concatenation in SystemVerilog? - system-verilog

I have the following question
Implement a circuit which shifts a 32-bit vector by two to the left and fills empty spots with zeros. Use only concatenation operator.
I'm just learning SystemVerilog, and I can't understand how I can use a concatenation operator for this. I would have just done 'assign y = a << 2' without concatenation ( with a declared as 32bit vector and y a 32 bit output), but I don't understand how can concatenation have anything to do with it.

Concatenation allows you to concatenate multiple parts of a vector with constants. As such, the left shift operator for a << 2 can be implemented as the following:
logic [7:0] x, y;
always_comb
y[7:0] = {a[5:0], 2'b0};
Just a concatenation of the lower 6 bits of a with 2 bits of 0 on the right hand side. Same what the left shift would do, but without any overflow.

Related

How would you write a SystemVerilog Function that adds an even parity bit to a 7-bit vector?

Would you take a 7-bit logic variable as an input and return an 8-bit logic output with the parity bit being the MSB (leftmost bit) of the output?
In a system that uses even parity you want the total number of 1 bits in the output vector to be even, right?
Can the the unary ^ operator can be used to calculate even parity?
Trying to figure out what that would look like?
I started something like this:
Code for parity
Sorry if this is more than one question.
I would do this like this:
input logic [N-1:0] data_in;
output logic [N:0] data_out;
assign data_out = {^data_in, data_in};
The ^ reduction operator does an XOR operation on all the bits in the operand. (So, it will return '1 if data_in has an odd number of ones.)

what is the meaning of point(.) when division operation in matlab

what is this code meaning?
k = round(Q/12. + Q/123.)-1;
I couldn't understand why that point(.) needed.
That code is from RSA code. Part of calculating coprime number.
The decimal point does not do anything here. It is probably the result of someone porting the code from another language with different data type conventions.
As hbaderts said, in Matlab the default numeric type is double precision; other numeric types must be explicitly set. You can test this yourself:
>> x = 123;
>> whos x
Name Size Bytes Class Attributes
x 1x1 8 double
You will often see the dot (.) preceding the division, multiplication, or power sign; there it means an elementwise operation.

Bit shifting a double value in MATLAB R2010b

I'm trying to perform a bit shift right operation on a double value in MATLAB 2010b. It seems that in newer MATLAB versions, this can be done using bitsra(), e.g.:
y = double(128);
bitsra(y,3)
but this function is not available in older versions.
What is the best way to achieve this?
You can use the bitshift function, which is available from at least MATLAB 2009a. From the documentation
c = bitshift(a, k) returns the value of a shifted by k bits.
When k is positive, 0-valued bits are shifted in on the right.
When k is negative, and a is unsigned, or a signed and positive, 0-valued bits are shifted in on the left.
When k is negative and a is a signed and negative, 1-valued bits are shifted in on the left.
On MATLAB 2012b
>> bitsra(128, 3)
ans =
16
On MATLAB 2009a:
>> bitshift(128, -3)
ans =
16
Edit: bitshift works with any fixed-point data type, although the error message generated by calling bitshift(128.5, -3) would suggest that it requires integer values. So bitshift(128.5, -3), for example, will not work since 128.5 is, by default, a floating point double precision variable. From the documentation for bitshift you can use the fi function from the floating-point toolbox to create fixed-point numbers. So to work with fractions one could do something like
>> bitshift(fi(128.5), -3)
ans =
16.025

In Matlab how to use variables for the numbers in scientific notation (matissa and exponent)?

I am using Matlab and using numbers in scientific notation, which are represented with the letter e for the exponent. An example in Matlab:
>> 2e5
ans =
200000
Now would like to work with numbers in scientific notation, but using variables to hold the values of the mantissa and the exponent (left and right side of the e respectively). I do not understand how this can be done without the variable names merging with the letter e for the exponent. Eg:
>> rr=5;
>> 2err
??? 2err
|
Error: Unexpected MATLAB operator.
Can this still be done? Or must I use the manual approach:
>> 2*10^rr
ans =
200000
You must use the manual approach; you can't use scientific notation like that with variables. You might want to use 2.*10.^rr, with the ., to enable you to use the same statement with arrays of numbers.

Understanding colon notation in MATLAB

So I'm completely new to MATLAB and I'm trying to understand colon notation within mathematical operations. So, in this book I found this statement:
w(1:5)=j(1:5) + k(1:5);
I do not understand what it really does. I know that w(1:5) is pretty much iterating through the w array from index 1 through 5, but in the statement above, shouldn't all indexes of w be equal to j(5) + k(5) in the end? Or am I completely wrong on how this works? It'd be awesome if someone posted the equivalent in Java to that up there. Thanks in advance :-)
I am pretty sure this means
"The first 5 elements of w shall be the first 5 elements of j + the first 5 elements of k" (I am not sure if matlab arrays start with 0 or 1 though)
So:
w1 = j1+k1
w2 = j2+k2
w3 = j3+k3
w4 = j4+k4
w5 = j5+k5
Think "Vector addition" here.
w(1:5)=j(1:5) + k(1:5);
is the same that:
for i=1:5
w(i)=j(i)+k(i);
end
MATLAB uses vectors and matrices, and is heavily optimized to handle operations on them efficiently.
The expression w(1:5) means a vector consisting of the first 5 elements of w; the expression you posted adds two 5 element vectors (the first 5 elements of j and k) and assigns the result to the first five elements of w.
I think your problem comes from the way how do you call this statement. It is not an iteration, but rather simple assignment. Now we only need to understand what was assigned to what.
I will assume j,k, w are all vectors 1 by N.
j(1:5) - means elements from 1 to 5 of the vector j
j(1:5) + k(1:5) - will result in elementwise sum of both operands
w(1:5) = ... - will assign the result again elementwise to w
Writing your code using colon notation makes it less verbose and more efficient. So it is highly recommended to do so. Also, colon notation is the basic and very powerful feature of MATLAB. Make sure you understand it well, before you move on. MATLAB is very well documented so you can read on this topic here.