What would be a sequence of boolean operations with the following truth table:
mask | target | result
======================
0 | 0 | 0
0 | 1 | 0
1 | 0 | 1
1 | 1 | 0
In a nutshell, that would be "toggle when mask bit is true, clear when mask bit is false".
Now some context:
I am designing a turning signal with Arduino, and I am setting current blinking lights with a bit mask, using just two bits:
typedef enum ACTIVE_LIGHTS {
NONE = 0, // 00
RIGHT_LIGHT = 1, // 01
LEFT_LIGHT = 2, // 10
BOTH = 3 // 11
};
Now one requirement is this: when I run, say, toggleLeft() method, I want to clear the right bit, and toggle the left one.
I tried both ways, but didn't work as desired (mask is always RIGHT_LIGHT or LEFT_LIGHT):
target ^= mask; //this toggles one side but doesn't turn the other off
target = mask; //this turns other side off, but never turns off same side
What about ANDing the light to turn off with 0 (X1 AND 10 = X0), and XORing the light to toggle with 1 (0X XOR 10 = 1X)?
Edit: final answer
Looks like the only time a 1 is output is when mask bit is 1 and input bit is 0, so the function is something like out = mask AND NOT in, for each bit.
Related
I'm completely new to coding and have almost no idea what I'm doing. Currently I'm trying to find the radius of each pixel from the center of a galaxy in a fits file. I was told to try this by creating a blank array the same size as the fits file and I'm trying to use a for loop for each x value and another for each y. So far, I have the array and have attempted to create the for loops.
xcenter =249.8
ycenter =250.0
d=fltarr(500,500)
for i=0,499 do begin
d=d(i-xcenter,*)
endfor
for j=0,499 do begin
d=d(j-ycenter,*)
endfor
I know this look awful and I obviously have no idea what I'm doing. So if anyone can offer any help at all I'd be grateful.
Let's look at a simpler version first. Suppose you have 10 points on a line, and you want to calculate the distance of each point from some xref. IDL supports vector math, so you can do this:
xref = 5.4
x = dindgen(10)
distance = abs(x - xref)
I have used the DINDGEN command here, and you can look up the help. Now in a 2d case, you want 2-d arrays: one will be a 500 * 500 array containing the X coordinate of each pixel, the other containing the Y coordinate. So the arrays will have to be of the form,
0 1 2 3 ...
0 1 2 3 ...
0 1 2 3 ...
and
0 0 0 0 ...
1 1 1 1 ...
2 2 2 2 ...
We can generate them using the # operator. Note that IDL counts from 0 by default.
just_one = replicate(1d, 500) ; contains 1 1 1 1 ...
one_500 = dindgen(500) ; contains 0.0 1.0 2.0 ...
x = just_one # one_500
y = one_500 # just_one
Now you can calculate the distance as usual, d = sqrt(xx + yy), but using vector math again:
distance = sqrt( (x - xref) ^ 2 + (y - yref) ^ 2 )
This is a 500x500 array, which contains the distance of each pixel from your xref, yref points.
I have an issue and i'm looping on it! :| I hope someone can help me..
So i have an input file (.xls), that is simple but there are a row (lets say its "ROW1") that is like this:
ROW1 | ROW2 | ROW3 | ROW_N
765 | 1 | AAAA-MM-DD | ...
null | 1 | AAAA-MM-DD | ...
null | 1 | AAAA-MM-DD | ...
944 | 2 | AAAA-MM-DD | ...
null | 2 | AAAA-MM-DD | ...
088 | 7 | AAAA-MM-DD | ...
555 | 2 | AAAA-MM-DD | ...
null | 2 | AAAA-MM-DD | ...
There are no stardard here, like you can see.. There are some lines null (ROW1) and in ROW2, there are equal numbers, with different association to ROW1 (like in line 5 and 6, then in line 8 and 9).
My objective is to copy and paste the values from ROW1, in the ROW1 after when is null, till isn't null. Basically is to copy form previous step, when is null...
I'm trying to use the "Formula" step, by using something like:
=IF(AND(ISBLANK([ROW1]);NOT(ISBLANK([ROW2]));ROW_n=ROW1;IF(AND(NOT(ISBLANK([ROW1]));NOT(ISBLANK([ROW2]));ROW_n=ROW1;ROW_n=""));
But nothing yet..
I've tried "Analytic Query" but nothing too..
I'm using just stream a xls file input..
Tks very much, any help is very much appreciiated!!
Best Regardsd!
Well i discover a solution, adding a "User Defined Java Class" with the code below:
import java.util.HashMap;
private FieldHelper output_field, card_field;
private RowSet out, log;
private String previou_card =null;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
if (first)
{
first = false;
out = findTargetRowSet("out");
output_field = get(Fields.Out, "previous_card");
} else {
Object[] r = getRow();
if (r == null) {
setOutputDone();
return false;
}
r = createOutputRow(r, data.outputRowMeta.size());
if (previous_card != null) {
output_field.setValue(r, previous_card);
}
if (card_field == null) {
card_field = get(Fields.In, "Grupo de Cartões");
}
String card = card_field.getString(r);
if (card != null && !card.isEmpty()) {
previous_card = card;
}
// Send the row on to the next step.
putRowTo(data.outputRowMeta, r, out);
}
return true;
After this i have to put a few steps but this help very much.
Thank you mates!!
Finally i got result. Please follow below steps
Below image is full transformation screen.
Data Grid Data will be like these. Sorry for that in my local i don't have Microsoft because of that i took Data Grid. Instead of Data Grid you can drag and drop Microsoft Excel Input step.
Drag and Drop one java script step and write below code.
Last step of transformation, drag and drop Select values step and select the columns.( These step is no necessary)
Final result will be like these.
Hope this helps.
I have this project listen below and im not sure where to start maybe someone can give me a few pointers or perhaps point me in the right direction of starting this?
Thanks!!
Input: A, B = octal digits (see representation below); Cin = binary digit
Output: S = octal digit (see representation below); Cout = binary digit
Task: Using binary FAs, design a circuit that acts as an octal FA. More specifically,
this circuit would input the two octal digits A, B, convert them into binary numbers, add
them using only binary FAs, convert the binary result back to octal, and output the sum as
an octal digit, and the binary carry out.
Input/Output binary representation of octal digits
Every octal digit will be represented using the following 8-bit binary representation:
Octal 8-bit Input Lines:
Digit: 0 1 2 3 4 5 6 7
0 1 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0
2 0 0 1 0 0 0 0 0
3 0 0 0 1 0 0 0 0
4 0 0 0 0 1 0 0 0
5 0 0 0 0 0 1 0 0
6 0 0 0 0 0 0 1 0
7 0 0 0 0 0 0 0 1
You are required to design the circuit in a structured way.
Ok, so essentially you're being asked to design a 8-to-3 encoder and a 3-to-8 decoder. Because you're given FAs to work with that's not the point of the assignment.
First we need to define how an encoder and decoder function. So we construct a truth table:
Encoder:
Input | Output
01234567 | 421
-----------------
10000000 | 000
01000000 | 001
00100000 | 010
00010000 | 011
00001000 | 100
00000100 | 101
00000010 | 110
00000001 | 111
and the decoder is just the reverse of that.
Next, how do we construct our encoder? Well, we can simply attack it one bit at a time.
So for the 1s digit we have if input bit 1, 3, 5 or 7 is set then it's 1, otherwise it's 0. So we just need a giant OR with 4 inputs connected to 1, 3, 5 and 7.
For the 2s digit we need the OR gate connected to 2, 3, 6, 7. Finally for the 4s gate, connect them to 4, 5, 6, 7. This doesn't do any error checking to make sure extra bits aren't set. Though, the behavior in that case seems to be undefined by spec, so it's probably OK.
Then you take your three lines and feed them to your adders. This is easy so I won't get into it.
Finally you need a decoder, this is a bit more tricky than the encoder.
Let's look at the decoder truth table:
Input | Output
421 | 01234567
----------------
000 | 10000000
001 | 01000000
010 | 00100000
011 | 00010000
100 | 00001000
101 | 00000100
110 | 00000010
111 | 00000001
This time we can't just use 3 or gates and call it a day.
Let's write this down in C-like code:
if (!input[0] && !input[1] && !input[2])
output[0] = 1
if (input[0] && !input[1] && !input[2])
output[1] = 1
if (!input[0] && input[1] && !input[2])
output[2] = 1
if (input[0] && input[1] && !input[2])
output[3] = 1
if (!input[0] && !input[1] && input[2])
output[4] = 1
if (input[0] && !input[1] && input[2])
output[5] = 1
if (!input[0] && input[1] && input[2])
output[6] = 1
if (input[0] && input[1] && input[2])
output[7] = 1
So, it looks like we're going to be using 8 3 input AND gates, and three NOT gates!
This one is a bit more complicated, so I made an example implementation:
If the conversion is to be done by hand in class, you can try the following way.
Conversion of Octal to Binary:
To convert octal to binary, replace each octal digit by its binary representation.
Example: Convert 518 to binary:
58 = 1012
18 = 0012
Therefore, 518 = 101 0012.
Conversion of Binary to Octal:
The process is the reverse of the previous algorithm. The binary digits are grouped by threes, starting from the decimal point(if present) or the last digit and proceeding to the left and to the right. Add leading 0s (or trailing zeros to the right of decimal point) to fill out the last group of three if necessary. Then replace each trio with the equivalent octal digit.
Example, convert binary 1010111100 to octal:
(Adding two leading zero's, the number is 001010111100)
001 = 1, 010 = 2, 111 = 7, 100 = 4
Therefore, 1010111100 = 1274
To convert to and from octal you can use an encoder & decoder pair (http://www.asic-world.com/digital/combo3.html). The 3 bit adder can be made from chaining the 3 FAs.
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
How can I do division by 2 in Binary Signed digit (Redundant Binary representation) ? Shifting won't work right ?
A redundant binary representation is just an expression of the form:
\sum_{i=0}^n d_i 2^n
where the d_i's are drawn from a larger set than just {0,1}.
Dividing by two or shifting right takes that to
\sum_{i=0}^{n-1} d_{i+1} 2^n + f(d_0)
The trick comes in how to deal with adjusting for the redundant representation for d_0.
If your RBR has digits the form {0,1,2} and has a 2 for the least significant digit you will then have to add 1 to the result to compensate, so f(0) = 0, f(1) = 0, f(2) = 1 should work.
4 = 12_base2, so 12_base2 >> 1 = 1 + f(2) = 1 + 1 = 2_base2 = 2 as expected.
6 = 102_base2, so 102_base2 >> 1 = 10_base2 + f(2) = 11_base2 = 3
You can get something similar for signed redundant binary representations (i.e. with d_i in {-1,0,1}) by setting f(-1) = -1.
1 = 1(-1)_base2, so 1(-1)_base2 >> 1 = 1 + f(-1) = 1 - 1 = 0
So ultimately the naive approach of just shifting does work, you just need a fudge factor to account for any redundant encoding of the shifted digits.
If your chosen RBR includes more options, you'll need to adjust the fudge factor accordingly.