Representing 2 binary digits in hex for vhdl - binary

In VHDL, If X'1 => "0001", X'3 => "0011". i.e, 1 hex digit represents 4 binary values, how do i represent only 2 binary values in hex given that i have only a specific bit range in memory. In this case 2. For instance, the space left in memory can only take 2 bits. I know i can still use the initial representation and mask out either the two msb's or lsb's but is there another way ?

You can do this if you are using VHDL-2008:
2X"2" = "0010"
Further examples from web:
unsigned notation (default):
7UX"F" = "0001111" -- extend
7UX"0F" = "0001111" -- reduce
signed noataion:
7SX"F" = "1111111" -- extend
7SX"CF" = "1001111" -- reduce

Related

Separating character and numeric variables without delimiter in SAS

I have below data with 5 variables with 5 observations: Name Age Gender Weight country
data have;
length string $30.;
input string$;
datalines;
Naresh30Male70India
Venkey29Male50Kenya
Ravi30Male56Pak
Sai67Female40iran
Divya89Female78Dubai
;
run;
I want to Separate these 5 variables in 5 observations
Help me on it
This is fixed code that should work for any range of ages/weights. Not the most elegant solution, but it works. But I would still rather fix that at the input site, and put some delimiters into the original string.
data want;
set have;
mixed2=substr(Mixeddata, anydigit(Mixeddata), anydigit(Mixeddata,-50)-anydigit(Mixeddata)+1);
mixed3=substr(mixed2, notdigit(mixed2));
name=substr(Mixeddata,1, anydigit(Mixeddata)-1);
age=substr(mixed2,1,notdigit(mixed2)-1);
gender=substr(mixed2, notdigit(mixed2), anydigit(mixed2, 5)-notdigit(mixed2));
country=substr(Mixeddata, anydigit(Mixeddata, -50)+1);
weight=substr(mixed3, anydigit(mixed3));
drop Mixeddata mixed2 mixed3;
run;
Consider the makeup of a single string:
Naresh30Male70India
Each desired column is either a number or a character. If we could break this out into two strings, one with only numbers and one with only characters, we can easily pull the needed values:
string_num: 30 70
string char: Naresh Male India
We can do this with regular expressions by replacing letters with spaces and numbers with spaces.
data want;
set have;
string_num = compbl(prxchange('s/[0-9]/ /', -1, string) );
string_char = compbl(prxchange('s/[a-zA-Z]/ /', -1, string) );
name = scan(string_num, 1);
age = scan(string_char, 1);
gender = scan(string_num, 2);
weight = scan(string_char, 2);
country = scan(string_num, 3);
drop string_num string_char;
run;
Note that we use the compbl function to remove any extra spaces to make it easier to read for learning purposes, but this is an optional step.

matrix operation not returning correctly

R = [cos(pi/3) sin(pi/3); -sin(pi/3) cos(pi/3)]
[i,j]=round([1 1] * R)
returns
i =
-0 1
error: element number 2 undefined in return list
While I want i=0 and j=1
Is there a way to work around that? Or just Octave being stupid?
Octave is not being stupid; it's just that you expect the syntax [a,b] = [c,d] to result in 'destructuring', but that's not how octave/matlab works. Instead, you are assigning a 'single' output (a matrix) to two variables. Since you are not generating multiple outputs, there is no output to assign to the second variable you specify (i.e. j) so this is ignored.
Long story short, if you're after a 'destructuring' effect, you can convert your matrix to a cell, and then perform cell expansion to generate two outputs:
[i,j] = num2cell( round( [1 1] * R ) ){:}
Or, obviously, you can collect the output into a single object, and then assign to i, and j separately via that object:
[IJ] = round( [1 1] * R ) )
i = IJ(1)
j = IJ(2)
but presumably that's what you're trying to avoid.
Explanation:
The reason [a,b] = bla bla doesn't work, is because syntactically speaking, the [a,b] here isn't a normal matrix; it represents a list of variables you expect to assign return values to. If you have a function or operation that returns multiple outputs, then each output will be assigned to each of those variables in turn.
However, if you only pass a single output, and you specified multiple return variables, Octave will assign that single output to the first return variable, and ignore the rest. And since a matrix is a single object, it assigns this to i, and ignores j.
Converting the whole thing to a cell allows you to then index it via {:}, which returns all cells as a comma separated list (this can be used to pass multiple arguments into functions, for instance). You can see this if you just index without capturing - this results in 'two' answers, printed one after another:
num2cell( round( [1 1] * R ) ){:}
% ans = 0
% ans = 1
Note that many functions in matlab/octave behave differently, based on whether you call them with 1 or 2 output arguments. In other words, think of the number of output arguments with which you call a function to be part of its signature! E.g., have a look at the ind2sub function:
[r] = ind2sub([3, 3], [2,8]) % returns 1D indices
% r = 2 8
[r, ~] = ind2sub([3, 3], [2,8]) % returns 2D indices
% r = 2 2
If destructuring worked the way you assumed on normal matrices, it would be impossible to know if one is attempting to call a function in "two-outputs" mode, or simply trying to call it in "one-output" mode and then destructure the output.

GNU Octave fail hex2dec

Encounter fail with some HEX to DEC conversions
b = '0x170d21b9'
bdec = hex2dec(b)
Return the
bdec = NaN
But should be 386736569
How to convert the hexadecimal number to integer in above example?
Leave off the 0x. This is a notation used in some languages to denote that the number is hex, but isn't actually part of the number. Octave's hex2dec function doesn't use that. Put b = '170d21b9' and you will get bdec = 386736569.

How to convert binary floating point numbers to decimal numbers with verilog?

I am writing a program. there is a binary floating number like this format : XX.XXX. for example,binary floating number 01.101 convert to decimal number is 1.625. I tried it for a long time, but couldn't work it out.
I use [4:0]num to store the number. num[4:3] is the integer part, num[2:0] is the floating part. the integer part is easy, when num[2:0]=3'b101, it means that the binary floating part is 0.101, and convert to decimal number is 0.625. so how can I convert the sequence"101", get a sequence "625"?
The quickest way is probably just to use a LUT (lookup table). Since the fractional portion is only 3 bits, that leaves you with only 8 possibilities. And you could use an 12bit value where each nibble is a digit that could be sent to a display etc.
reg result[11:0] // each nibble represents a digit
always #(*) begin
case (num[2:0])
3'b000 : result = 12'h000;
3'b001 : result = 12'h125;
3'b010 : result = 12'h250;
3'b011 : result = 12'h375;
3'b100 : result = 12'h500;
3'b101 : result = 12'h625;
3'b110 : result = 12'h750;
default : result = 12'h875;
endcase
end

Denary to binary conversion program

How does this denary to binary program work? I am finding it hard to comprehend what is happening behind the code.
Can someone explain the lines 6 onwards?
Number = int(input("Hello. \n\nPlease enter a number to convert: "))
if Number < 0:
print ("Can't be less than 0")
else:
Remainder = 0
String = ""
while Number > 0:
Remainder = Number % 2
Number = Number // 2
String = str(Remainder) + String
print (String)
The idea is to separate out the last part of the binary number, stick it in a buffer, and then remove it from "Number". The method is general and can be used for other bases as well.
Start by looking at it as a dec -> dec "conversion" to understand the principle.
Let's say you have the number 174 (base10). If you want to parse out each individual piece (read as "digit") of it you can calculate the number modulo the base (10), then do an integer division to "remove" that digit from the number. I.e. 174%10 and 174//10 => (Number) 17|4 (Reminder). Next iteration you have 17 from the division and when you perform the same procedure, it'll split it up into 1|7. On the next iteration you'll get 0|1, and after that "Number" will be 0 (which is the exit condition for the loop (while Number > 0)).
In each iteration of the loop you take the remainder (which will be a single digit for the specific base you use (it's a basic property of how bases work)), convert it to a string and concatenate it with the string you had from previous iterations (note the order in the code!), and you'll get the converted number once you've divided your way down to zero.
As mentioned before, this works for any base; you can use base 16 to convert to hex (though you'll need to do some translations for digits above 9), octal (base 8), etc.
Python code for converting denary into binary
denary= int(input('Denary: '))
binary= [0,0,0,0]
while denary>0:
for n,i in enumerate(binary):
if denary//(2**(3-n))>=1:
binary[n]= 1
denary -= 2**(3-n)
print(denary)
print (binary)