Perfmon, how to combine Combine FirstValueA and FirstValueB? - sql-server

I am using performance monitor to collect the counters data and save it to the DB. Here is the DB structure defined in the msdn http://msdn.microsoft.com/en-us/library/windows/desktop/aa371915(v=VS.85).aspx
Based on DB structure, here is the definition of the FirstValueA:
Combine this 32-bit value with the value of FirstValueB to create the
FirstValue member of PDH_RAW_COUNTER. FirstValueA contains the low
order bits.
And the FirstValueB:
Combine this 32-bit value with the value of FirstValueA to create the
FirstValue member of PDH_RAW_COUNTER. FirstValueB contains the high
order bits.
The fields FirstValueA and FirstValueB should be combined to create the FirstValue, and similarly the SecondValue.
How do you combine FirstValueA and FirstValueB to get the FirstValue in SQL Server?

So what they're saying is that you need to comingle the two, like this:
//for reference, this is 32 bits
12345678901234567890123456789012
000000000000000000000FirstValueA
000000000000000000000FirstValueB
What it says is we need to combine the two. It says that A is the low order, and B is the high order.
Let's refer to Wikipedia for http://en.wikipedia.org/wiki/Least_significant_bit and see that the low order is on the --> right, and the high order is on the <-- left.
low order -> right
high order <- left
A -> right
B <- left
So we're going to end up with (our previous example)
//for reference, this is 32 bits
12345678901234567890123456789012
000000000000000000000FirstValueA
000000000000000000000FirstValueB
becomes
//for reference, this is 32 bits
12345678901234567890123456789012
000000000000000000000FirstValueB000000000000000000000FirstValueA
Now, that doesn't work if the values look like this:
//for reference, this is 32 bits
12345678901234567890123456789012
1001101100110100101011010001010100101000010110000101010011101010
//the above string of 1's and 0's is more correct for the example
What you're given is not two binary strings, but two integers. So you have to multiply the left value by 2**32 and add it to the right value. (that's a 64 bit field by the way)
let's examine tho, why the low order bit is on the right and the high order is on the left:
Binary is written just like Arabic numerals. In Arabic numerals, the number:
123456
means one hundred twenty three thousand, four hundred fifty six. The one hundred thousand is the most significant part (given as we would shorten this to "just over one hundred thousand dollars" instead of "a lot over 6 dollars") and the six is the part we most freely drop. So we could say that the number were:
123 is the value that contains the high order bits, and 456 is the value that contains the low order bits. Here we would multiply by 10^3 to add them together (this is a mathematical fact, not a guess, so trust me on this) because it would look like this:
123
456
and so the same works for the binary:
//for reference, this is 32 bits
12345678901234567890123456789012
000000000000000000000FirstValueB
000000000000000000000FirstValueA
tl;dr:
Multiply B by 2^32 and add to A

Console.WriteLine("{0} {1} {2} : {3} {4}", p.CategoryName, p.InstanceName, p.CounterName, p.RawValue, p.CounterType.GetHashCode());
float FirstValue = p.NextValue();
Console.WriteLine("FirstValueA :{0}", (ulong)FirstValue & 4294967295);
Console.WriteLine("FirstValueB :{0}", (ulong)FirstValue >> 32);
Console.WriteLine("SecondValueA :{0}", p.NextSample().TimeStamp & 4294967295);
Console.WriteLine("SecondValueB :{0}", p.NextSample().TimeStamp >> 32);

Related

Generate permutations with k fixed bits

Suppose I have several N-bit numbers where K (1 < K < N) bits are fixed (i.e. 0 or 1). My goal is to generate all possible permutations.
Example: N = 3, K = 1 (middle bit is fixed to '0'). Then possible permutations are
000
001
100
101
Let's say I have number X=000 and array fixed={-1,0,-1} that stores information of fixed bits (-1 = bit not fixed, 0 or 1 = fixed).
Simple solution is to generate all permutations
000,001,...,111 and loop through each one bit by bit and test whether all fixed bits have correct value (stored in fixed). If at least one fixed bit differs from the corresponding value in fixed, then this permutation is removed from the result.
This is, however, inefficient because it takes 2^N instead of 2^(N-K) permutations. Is there an algorithm or approach to this problem that needs only 2^(N-K) permutations (which are directly in the result)?
Simple bit trick allows to solve this problem effectively.
Make binary masks:
A where all fixed bits are cleared (both fixed zeros and fixed ones!) and other bits are set
B where fixed ones are set
for example, x01x gives A = 1001, B = 0010
Traverse all submasks of A and set fixed ones with B before output:
sm = A
repeat:
out = sm or B
//use out bit combination
sm = (sm - 1) & A
until (sm = 0)
This method generates all needed bit combinations without excessive steps

Using binary strings on storing ordered items in database

In this post, #boisvert mentioned that if using string as the order field's value, it is best shown for a binary string, and then gave an algorithm to calculate the average of two binary strings as follows:
Avalue = 1+0*(1/2)+1*(1/4)+1*(1/8)
Bvalue = 1+1*(1/2)+0*(1/4)+0*(1/8)
average, new value = 1+0*(1/2)+1*(1/4)+1*(1/8)+1*(1/16) new string = "10111"
content order
--------------------
A '1011'
new! '10111'
B '1100'
C '1101'
I couldn't understand these very well, what's the value of the first item putting into the DB and the items inserting before/after it? How to calculate the average between '1011' and the new value '10111', or between '111' and '1000'?
Any help is much appreciated.
The binary strings are fractions, not integers; the decimal point is always at the beginning (or after the first digit, in #boisvert's answer; it doesn't make any difference as long as the position of the decimal point is fixed. Of course, it's actually a binary point since these are binary numbers.)
To find the average:
If the strings differ in length, put enough 0s at the end of the shorter string so that it is the same length as the longer string.
Add the two strings together, using binary addition, always putting the last carry at the beginning, even if it is ´0'. [See algorithm below].
Remove any 0s at the end.
Example 1: 1011 and 10111
Extend the first string with a 0: 10110 and 10111
Find the sum:
A: 10110
B: 10111
Carry: 101100
Sum: 101101
No trailing zeros, so the result is 101101
Example 2: 111 and 1000
1. 1110 1000
2. 10110
3. 1011
Starting off and insertion at the end:
The first item put into the database has the label 1. If at any point you need to add an item at the very beginning, use the first label with a 0 before it. Similarly, if you need to add an item at the end, use the first label with a 1 before it.
Binary addition:
Since the strings are the same length, this is easy; set Carry to 0, and scan both strings from back to front. (The output is also produced back-to-front.)
At each position:
* If the sum of Carry and the two digits is 1 or 3, output a 1, otherwise output a 0.
* If the sum of Carry and the two digits is 2 or 3, set Carry to 1, otherwise set it to 0.
When you've finished all the digits, output the value of Carry.
Practical implementation:
In practice, you wouldn't use binary strings; you'd use some fairly large base, the only requirement being that it is even. But the algorithms are the same. When constructing the representation of your numbers, you need to assign digits to characters in alphabetical order, so that the resulting strings can be sorted alphabetically without converting them to numbers; the database doesn't know how to convert to numbers, but it knows how to sort strings alphabetically.

Order insensitive hash function for an array

I'm looking for a hash-function which will produce the same result for unordered sequences containing same elements.
For example:
Array_1: [a, b, c]
Array_2: [b, a, c]
Array_3: [c, b, a]
The hash-function should return the same result for each of these arrays.
How to achieve this?
The most popular answer is to sort elements by some rule, then concatenate, then take hash.
Is there any other method?
if a,b,c are numbers, you could sum up and then build a hash on the sum.
You may multiply, too.
But take care about zeros!
XOR-ing numbers is also an approach.
for very small numbers you may consider to set the bit indexed by the number. This means building a long (64bit) as input for the hash allows only element numbers in range 0-63.
The more elements you have the more collisions you will get.
In the end you map n elements with m bits (resulting to 2^(m*n) range) to a hash value with k bits.
Usually m and k is a constant but n varies.
Please aware any access as by a hash requires a test whether to get the correct element. In general a hash is NOT unique.
otherwise sort the element and then do the hash as proposed
Regarding the comment from CodesInChaos:
in order to be able to omit a test, the numbers of bits of the hash should be much greater than the sum of elements bits. Say at least 64 bits more. In general this situation is not given.
One common case of secure hash/unique id is a guid. This means effectively 128 bits.
A random sequence of text char reaches this number of bits within 20-25 characters.
Longer texts are very likely to produce collisions. It depends on the use case whether this is still acceptable.
XOR | Sum | Sum of squares | ...
where | denotes concat.
or
XOR of hash of elements

Bit Operations with Enumerated Type

I'm having some trouble getting started with a lab of mine. The lab has the following instructions:
Given a value int input = 0b10010110 determine what sports an individual likes. Assume there will be no errors in this input. From left to right, each position represents the following: Football, Baseball, Volleyball, Swimming, Softball, Soccer, Hockey, Lacrosse.
If there is a 1 is in that position, then the person likes that sport. Based on the “binary” input given, output to the screen all of the sports that the user enjoys. For the given binary number, the user likes, football, swimming, soccer and hockey.
Do not make an array of characters.
Be sure to use an enumerated data type.
I'm not sure how I can compare each position of the string to tell whether it is a 1 or 0. One idea I have is to use an enumerated type where I set each sport to a ten digit number where only its appropriate position is 1
enum sport{
Football = 0010000000,
Baseball = 0001000000,
Volleyball = 0000100000,
... ,
Lacrosse = 0000000001
};
I would then shift left/ right on the given value "input" the appropriate amount of times to leave only the specified position with its original value and to set all other values to 0. For Football:
input << 2; input >> 9; input << 7;
So the new set value would be 0010000000. Then I'd be able to compare the number as a whole. I would have to that for each case, but I cannot think of a different way to do it.
Am I completely off? Is there a more functional way to check the value of each position using bit operations? Thanks for any assistance in advance.
Use bitwise AND operator &:
if (input & Football) {
// Football bit is set
} else {
// Football bit is not set
}
Also note that the 0b prefix is a compiler extension but is not standard C. And in your enum values, a number starting with the 0 prefix is in octal format (you have to fix this). I suggest you to use hexadecimal format for bit manipulation.
You could make things a bit more readable if you define you sport values by shifting a 1 to the left by the appropriate number of places; in fact, you could use a simple enumeration for the sport values, and use that to specify how many places to shift the bit.

What is the difference between bitmask and bitmap in C

What is the conceptual difference between them? I know bitmap is sort of bitfields in the structs..
struct{
int bit1: 1;
int bit2: 1;
int bit3: 1;
};
so in tht case is bitmask something we define for an enum?
A bitmask is an integer type that is used to "mask" certain bits when performing bitwise operations. For example, the bitmask 0xFFFFFFFF might be used to mask a 32-bit unsigned value because you want to operate on all bits at once, whereas 0x00000001 would only operate on the very last bit. You often see bitmasks defined as the 'flipped' version and then flipped using ~.
A bitmap, on the other hand, is a set of variables each mapped to an individual bit. There are many ways of achieving this, your struct is one (common) example of a bitmap.
You might put various masks in an enum to give yourself easier access to them, but it's not strictly necessary to do so.
Bitmap is more of data itself which comprise of bits.
One of the example could be say that there are 8 friends in a group and group performs various activities together.
Group participation in each activity now can be represented by "bitmap" which comprise of bits (each for one friend).
e.g.
Skii - 10110000 <<<<friend 5,6 and 8 will go to Skii
Movie - 10011000 <<< friend 4,5 and 8 will go to movie
College- 11111111 <<<all friends will go to college
Bitmask is more skeleton for bitmap. bitmask will be used to set and get bit values in bitmap.
friend1 - 00000001<<<< bitmask for friend 1
friend2 - 00000010 <<<bitmask for friend 2
friend5 - 00010000
(Note: I found it awkward to address friend0 :) , purist may consider everything as n-1)
Now using bitmap and bitmask we can determine
is friend1 going for Skii?
Skii & friend1 <<<< this would be zero
is friend 5 going to Movie?
Movie & friend5 <<< yes

Resources