When I do the following logical AND operation with numbers in C# I'm getting the following results:
-3 & 3 = 1
-1 & 1 = 1
0 & 0 =0
but when I do 8 & -8 =8
Can someone please explain how we are getting the result as 8?
If you look at the numbers in hexadecimal format, it will help you understand how the calculation is performed.
Assuming you store the numbers as integers:
3 = 0x00000003
8 = 0x00000008
-3 = 0xFFFFFFFD
-8 = 0xFFFFFFF8
Then if we zoom in on the smallest nibble (4-bits, consider the following):
For 3 & -3
0 0 1 1
& 1 1 0 1
-------
0 0 0 1 = 1
For 8 & -8
1 0 0 0
& 1 0 0 0
-------
1 0 0 0 = 8
To see what is going on, you could use the following:
static void Main(string[] args)
{
show(8);
show(-8);
}
static void show(int i)
{
Console.WriteLine($"{i,3} = 0b{Convert.ToString(i, 2).PadLeft(32, '0')}");
}
Output:
8 = 0b00000000000000000000000000001000
-8 = 0b11111111111111111111111111111000
Related
Im searching for an algorithm which could get me the closest point, in a 2D Array/Matrix, as fast as possible.
Example Matrix:
1 2 3 4 5 6 7 8 9 10 11
________________________
1: 0 0 0 0 0 0 0 1 0 0 0
2: 0 0 0 0 0 0 0 0 0 0 1
3: 1 0 0 0 0 0 0 0 0 0 0
4: 0 0 0 0 0 X 0 0 0 0 0
5: 0 0 0 0 0 0 0 0 0 0 0
6: 0 0 0 1 0 0 0 0 0 0 0
7: 0 0 0 0 0 0 0 0 0 0 0
X = my Point. 1 = Point im searching for.
In this example the "1" on row 6 is the one im searching for.
Right now im scanning the entire array, and calculate for each "1" which is closest to X, but it seems slow to me.
I thought about an searchalgorithm that starts on the fields next to the X, takes circular steps, and breaks when a "1" is found.
Does someone have any idea how to realize this?
Edit: Distance to a point is defined by sqrt( (x1-x2)^2 + (y1-y2)^2 ). So the distance to the „1“ in row 6 is 2,83.
Edit: To be concrete: this will be used to do a pixelsearch in an image. Startpoint is in the middle, and searching for the closest „correct pixel“.
thanks!
I am writing a C# program that modifies a binary file. In the file there is a byte that stores ALL the information on what a person is wearing.
Example:
1 = Hat
2 = Shoes
4 = Socks
8 = Pants
16 = Shirt
32 = Glasses
64 = Watch
128 = Earrings
Sally is wearing shoes, pants and a shirt = 2 + 8 + 16 = 26. The byte stored is 26.
Fred is wearing a hat, shoes, socks, paints, shirt, glasses and a watch: 1 + 2 + 4 + 8 + 16 + 32 + 64 = 127. The byte stored is 127
Now I want to take that number and figure out what they are wearing. A person cannot wear two of the same things, and there are only the 8 options.
You have a bit mask.
Using your 2 + 8 + 16 = 26 example, you can pull out each bit using the bitwise "and" operator &. To check if the person is wearing shoes, "and" the bit mask with 2 and check the result:
011010 = 26
& 000010 = 2 <-- bitwise "and" operator
-------------
000010 = 2
If the bitmask was 5 instead of 26, the result would be:
000101 = 5
& 000010 = 2 <-- bitwise "and" operator
-------------
000000 = 0
So take the result and check if it's greater than zero. That's it:
bool isHat = bitMask & 1 > 0;
bool isShoes = bitMask & 2 > 0;
bool isSocks = bitMask & 4 > 0;
//and so on
FYI: I'm guessing that you are setting your bit mask by adding powers of two to an accumulator like this:
byte SetWatch(byte bitMask) {
return bitMask + 64;
}
You can also use a bitwise operation to do this. Use the bitwise "or" like this:
byte SetWatch(byte bitMask) {
return bitMask | 64;
}
Use an enum with the [Flags] attribute, then use the HasFlag method to determine whether a given instance of the enum has that flag set.
https://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx
You could use this technique.
Referencing your example with Sally:
26 / 2 = 13 , Remainder = 0 <-- Hat
13 / 2 = 6 , Remainder = 1 <-- Shoes
6 / 2 = 3 , Remainder = 0 <-- Socks
3 / 2 = 1 , Remainder = 1 <-- Pants
1 / 2 = 0 , Remainder = 1 <-- Shirt
You can use bit-wise operators to figure this out.
var outfit = 26; //this is the same as 2 & 8 & 16
var bIsWearingPants = ((outfit | 8) != 0);
I know that my question will be a littel strange but i need realy some help. I have an algorithme to calculate true pixel quantity in a binary image. Her is an example how it work:
Binary Image :
0 0 1 0 0 1
1 1 1 1 0 1
1 1 1 1 1 1
1 1 0 0 0 1
0 1 0 0 0 1
Her is the result :
18 15 11 8 6 5
16 13 9 7 5 4
11 9 6 5 4 3
5 4 2 2 2 2
2 2 1 1 1 1
And this is how it work :
Result (i,j) = result (i+1, j) + result (i, j+1) - result(i + 1, j + 1) + Image(i,j)
Her an example for the 18 value:
18 = 16 + 15 - 13 + 0
My question :
What is the name of this algorithm because I need to get some more information about it?
Thank you for help.
This is called an integral image, or summed area table. It is used to speedup box filtering, among others. It is a 2D generalization of a prefix sum.
One of the basics of Computer Science is knowing how numbers are represented in 2’s complement. Imagine that you write down all numbers between A and B inclusive in 2’s complement representation using 32 bits. How many 1’s will you write down in all ?
Input:
The first line contains the number of test cases T (<=1000). Each of the next T lines contains two integers A and B.
Output:
Output T lines, one corresponding to each test case.
Constraints:
-2^31 <= A <= B <= 2^31 - 1
Sample Input:
-2 0
-3 4
-1 4
Sample Output:
63
99
37
Explanation:
For the first case, -2 contains 31 1’s followed by a 0, -1 contains 32 1’s and 0 contains 0 1’s. Thus the total is 63.
For the second case, the answer is 31 + 31 + 32 + 0 + 1 + 1 + 2 + 1 = 99
for (int i=1; i<line[0]; i++)
{
int numOf1s = 0;
for (int j=line[i].A; j<=line[i].B; j++)
{
for (unsigned int n=j; n>0; n>>=1)
numOf1s += n & 1;
}
printf("%d\n",numOf1s);
}
I'm reading the book:
Introduction to Neural Networks for C# Second Edition by Jeff Heaton
In particular the chapter about Hopefield network. He explains how to calculate the contribution matrix given a boolean array as pattern.
For exaple given the following pattern 0101, the corresponding contribution matrix (of weights) is:
0 -1 1 -1
-1 0 -1 1
1 -1 0 -1
-1 1 -1 0
The process of recognize the pattern follow this rule:
We must now compare those weights with the input pattern of 0101. We
will sum only the weights corresponding to the positions that contain
a 1 in the input pattern. The results of the activation of each neuron
are shown below.
N1 = -1 + -1 = -2
N2 = 0 + 1 = 1
N3 = -1 + -1 = -2
N4 = 1 + 0 = 1
These values are meaningless without an activation function. The
activation function used for a Hopfield network is any value greater
than zero, so the following neurons will fire.
N1 activation result is –2; will not fire (0)
N2 activation result is 1; will fire (1)
N3 activation result is –2; will not fire(0)
N4 activation result is 1; will fire (1)
As you can see, we assign a binary value of 1 to all neurons that
fired, and a binary value of 0 to all neurons that did not fire. The
final binary output from the Hopfield network will be 0101. This is
the same as the input pattern.
Moreover he said:
If we also want to recognize 1001, then we would calculate both
contribution matrixes and add the results to create the connection
weight matrix
So I calculate the second contribution matrix:
0 -1 -1 1
-1 0 1 -1
-1 1 0 -1
1 -1 -1 0
And add the two matrixes:
0 -2 0 0
-2 0 0 0
0 0 0 -2
0 0 -2 0
And obviously (following the previous rule) this last matrix cannot recognize any of the previous patterns. How is it possible? Where is the error?
EDIT: (Added the example provided by the author)
Considering the example provided, the two patterns are:
1100 -> [1 1 -1 -1]
0 1 -1 -1
1 0 -1 -1
-1 -1 0 1
-1 -1 1 0
1000 -> [1 -1 -1 -1]
0 -1 -1 -1
-1 0 1 1
-1 1 0 1
-1 1 1 0
Addition:
0 0 -2 -2
0 0 0 0
-2 0 0 2
-2 0 2 0
Multiply by [1 1 -1 -1]:
4 0 -4 -4
Multiply by [1 -1 -1 -1]:
4 0 -4 -4
In both cases the pattern recognized is 1000 (1100 is missing). Thus, there is something not working here.
This source doesn't look very good. For example it uses the term "inverse" instead of "transpose". Also the algorithm for recalling patterns is described incorrectly. Fortunately if you look at their implementation it seems to work fine (although it's also a low quality code).
The difference is that when you present a vector to recall a pattern for, you should also convert it to bipolar form and then calculate the dot product with each column of the weight matrix. So, in your example when you'll present the vector 1001 you calculate:
|0 -2 0 0|
[1 -1 -1 1] * |-2 0 0 0| = [2 -2 -2 2]
|0 0 0 -2|
|0 0 -2 0|
After applying the threshold function it yields the correct result: 1001. For the second vector, 0101:
|0 -2 0 0|
[-1 1 -1 1] * |-2 0 0 0| = [-2 2 -2 2]
|0 0 0 -2|
|0 0 -2 0|
which also gives correct result: 0101.
EDIT:
In your second example you seem to have reached the limit of Hopfield network capabilities. First of all, the patterns you present differ with only one bit, which makes them hard to tell apart. This, combined with the fact that Hopfield Network's capacity is said to be something around 0.138 * n (link), n being the number of neurons seems to explain the problem.
Other sources, like this one (Chapter 6) provide theoretical bound of n/2 * log(n) - for "almost all patterns" to be retrieved without errors. In this link you can also find other learning rules. And you can find another simple example here.