I want to count the number of 1's in an 8-bit input and output how many ones are in it. The way I am finding this is very crude and redundant. I want to know if there is any easy and good way of finding them. My code looks like:
module my_8to4bit(in,out);
input [7:0]in;
output [3:0]out;
assign out=(input == 1 || input == 2 || input == 4 || input == 8 || input == 16 || input == 32 || input == 64 || input == 128)?1:
(input == 3 || input == 5 || input == 6 || input == 9 || input == 10 || input == 12 || input == 24 || input == 128)?2:0;
... same goes upto all 1's in 8bit input.
Is there an easy way of finding them?
How about
always #* begin
out = 0;
for(i=0;i<8;i=i+1) begin
out = out + in[i];
end
end
Should just synthesize to 8 adders, one for each bit.
If you don't need to synthesize the code, and your simulator supports SystemVerilog syntax, you can use the $countones system function. Refer to the IEEE Std 1800-2009, for example.
You can look up answers in Bit Twiddling Hacks. If speed is important and space is not an issue, you might consider a 256-byte lookup table. Otherwise, probably use Brian Kernighan's way (and measure whether it is actually slower than the lookup table; it may be faster than the lookup table if memory is slow and the CPU is fast).
Related
Hi I'm pretty new to using Igor Pro. I'm looking for some help on writing a procedure for a task.
I have 4 waves, two are text waves and two are numeric waves(one of which has no data yet). I need to write a function that will compare the two text waves and if they are equal, have igor pull data from one of the numeric waves and put it in the correct point to match the text wave it's coupled with.
To make it visually conceptually
twave1 twave2
nwave1 nwave2
twave1 is a list of all isotopes up to neptunium but they're not in order, and nwave1 is their corresponding mass values. (both on table1)
twave2 is the same list of isotopes but ordered properly (i.e. 1H, 2H, 3H, 4H...3He, 4He...ect) and nwave2 is empty (both on table2)
so the goal is to create a function that will sort through twave1 and twave2, and if they match, pull the data from nwave1 into nwave2, so that the masses match with the correct isotopes on table2. So table2 will have the correctly ordered isotopes and now the mass data as well, in the correct places.
Any help would be greatly appreciated; this is where I've gotten so far
function assignMEf()
wave ME, ME_FRIB
wave isotope_FRIB, isotope
variable len = numpnts(ME)
variable i, j
variable ME_current, iso_current
for(i=0; i<len; i+=1)
ME_current = ME[i]
iso_current = isotope[i]
for(j=0; j<4254; j+=1)
if(iso_current == isotope_frib[j])
ME_frib = ME[i]
endif
endfor
endfor
end
If I understood correctly, the two waves you want at the end are isotope and ME. Your code was close to working, however you need to tell Igor when you declare a text wave that it is a text wave, by using the /t flag. I simplified the code a bit more:
function assignMEf()
wave ME, ME_FRIB
wave/t isotope, isotope_FRIB
variable len = numpnts(ME)
variable i, j
for(i = 0; i < len; i += 1)
for(j = 0; j < len; j += 1)
if(stringmatch(isotope[i],isotope_frib[j]))
ME[i] = ME_FRIB[j]
endif
endfor
endfor
end
This code is not very robust but works for what you'd like to do.
To test the code, here is my MWE:
•Make/O/N=(10) ME_FRIB = p
•Make/O/N=(10) ME = NaN
•Make/O/N=(10)/T isotope_FRIB = "iso" + num2str(10 - p)
•Duplicate/O isotope_FRIB,isotope
•Sort isotope,isotope
•Edit isotope_FRIB,ME_FRIB,isotope,ME
•assignmef()
I don't think stringmatch is the right choice here. It uses wildcard matching but the OP AFAIU wants match/no-match so !cmpstr is a better choice.
I am creating a game where it plays to 10 points to win and you can also win if a player reaches 7 points without the other player receiving any.
I have an if statement inside a while statement and was wondering if there was a way to combine them into one general statement.
I have tried combining them with an extra set of parenthesis on for the if portion as well as tried to change up the and/or boolean values to see if I got those wrong.
while (count1 <= 10 and count2 <= 10):
if (count1 == 7 and count2 == 0) or (count2 == 7 and count1 == 0):
Happy Path: The while and if loops combine into one statement, while still keeping the rules stated in the summary.
Currently: I have tried a bunch of combinations but they all go to an else statement or go beyond the 10 point limit, which tells me that the while parameters are wrong.
Try:
while (count1 <= 10 and count2 <= 10) or ((count1 != 7 or count2 != 0) and (count2 != 7 or count1 != 0)):
I'm using following logic laws in here:
not (a or b) <=> not a and not b
and
not (a and b) <=> not a or not b
I can't figure out how to take the total number of hours and split it into regular hours and overtime hours.
What I would like to achieve is to: add a module that splits the number of hours worked into regular hours and overtime hours. This module must have one ‘in’ parameter and two ‘out’ parameters.
What I could come up with was:
if (total_hours >= 0 || total_hours <= 40) {
reg_hours = total_hours;
}
if (total_hours >= 41 || total_hours <= 60) {
ovt_hours = (total_hours - 40);
}
total_hours is my input. And reg_hours and ovt_hours are my outputs. If I put in 35.6 for total_hours, it'll compile and give me -4.40. It does fine with an input of 40 or higher, but when it comes to anything less than that it always produces a negative ovt_hours value. I'm really confused on how to stop it from giving me a negative number. I've been searching everywhere to find a solution, but I couldn't find anything that could help me understand what I am doing wrong. It would help me greatly if anyone could explain this in the simplest way possible.
You can work up to 40 regular hours...
int reg_hours = max(total_hours, 40);
If you've worked more than 40 hours, then any extra hours are overtime hours...
int ovt_hours = 0;
if (total_hours > 40) {
ovt_hours = total_hours - 40;
}
There is one thing you should understand:
Using "if" statement with another "if" statement (without "else if" or "else") would definitely make your compiler go to both "if" statements (assuming the conditions are true).
Also, you should notice that in your code there is a logic mistake:
"if (total_hours >= 0 || total_hours <= 40)"
"if (total_hours >= 41 || total_hours <= 60)"
When you input "total_hours=35.6", the compiler with go into both if statements. Why? Because 35.6 is >= 0 and <= 40 (1st if statement) and 35.6 is <= 60 (2nd if statement).
Therefore you MUST use "else if" for your 2nd if statement.
Another thing is that, you might need to consider to use "&&" instead of "||" since you are checking a range between 2 values. Using "||" means you are only checking a range lower/higher than a value.
I have tried to write a small program that converts a decimal to binary that doesn't use the inbuilt function that do that. My program won't convert anything over 12287. 12288 just spits out an infinite loop. Where have I gone wrong? why can't I get above 12287?
while (number != 1) or (number != 0):
a = number // 2
b = number % 2
number = a
if b == 0:
output = "0" + output
if number == 1:
output = "1" + output
break
else:
output = "1" + output
You just need to change the condition from:
while (number != 1) or (number != 0):
To:
while number != 0:
Other than that, your code looks OK. Here is a more streamlined version of the same basic idea:
output = ""
while number:
number, b = divmod(number, 2)
output = str(b) + output
For 12288, number eventually becomes 0. This means while condition is always True.
Note that b becomes 0, so number will stay at 0.
i'm really new to Python and am completely stuck
is there any way to make the less than value a variable
for example
x = int(input ("Input a value for x: "))
i = 1
while i < x:
x += i
i += 1
else:
print ("here is your new number:", x,)
whenever i use this, nothing happens
thanks for your help
It's not technically true to say that nothing happens, plenty is happening.
However, one thing that's not happening is the thing that generates the output (unless you enter a value less than or equal to one).
The while..else construct will only execute the else block if the while block did not do any iterations.
So, if you want output after the loop regardless of whether the loop body executes, get rid of the else and unindent the print.
The other thing that's not happening is the exit condition of the loop itself. If i starts out less than x (and they're both positive to start with), adding i to x then adding 1 to i will never give you a situation where i is not less than x.
Think about (for example):
x i Description
----- ----- -----------
3 1 Initial state
4 2 x += i; i += 1;
6 3 x += i; i += 1;
9 4 x += i; i += 1;
13 5 x += i; i += 1;
18 6 x += i; i += 1;
24 7 x += i; i += 1;
31 8 x += i; i += 1;
You can see that x is increasing at a faster rate than i, hence i < x will always be true.
How you fix that depends entirely on what you're trying to achieve. Since you have described your problem in terms of the code, your code matches perfectly your requirements. Hence, since you state it's not working as you expected, it appears your requirements may need some work :-)
I would suggest stating, in English, what you're trying to achieve here, and we can then suggest where your implementation has gone wrong.
What you wrote will result in an infinite loop if i < x in the beginning. So it will never reach the print statement that you hope it will. Plus, I also believe that you have to delete the else and indent the print statement.