I can't get this line to run in constant time on my micro-controller:
int zz,yy; //some binary variables
zz = (yy) ? 0 : (1 & zz);
I tried to change it to
zz = (yy) ? (0 & zz) : (1 & zz);
because & should force an evaluation of the right side even though the left side defines the result as far as I know. But it did not help.
Can anyone suggest me a solution how to make this line constant time?
It entirely depends on your compiler and its settings. Maybe (!yy) *
(1 & zz) helps?
#Quentin
Related
I am new to C programming, in fact programming at all, and recently learn the use of macros with regard to preprocessor directives. Although I am getting more familiar with it the following exercise that I got from a textbook stumbles me since I do not get the solution or the general "take-away lesson" from it.
Before I wrote this question here, I tried to execute the code myself by adding some printf() in order to obtain the correct answers but it does not even compile. Now, before I write down the question and code I want to make it explicit that this is a self-learning question and that I do not want to offend people here with a question that many will find trivial. I just want to understand what is going on.
The code is as follows
int x=2, y=3, a=4,b=5;
#define MAX(x, y) x > y x : y
int c,d,e,f;
c = MAX( a, 3 );
d = MAX( y, x );
e = MAX( ++x, 1 );
f = MAX( b, MAX (6, 7) );
I am asked to give the values of c,d,e and f. There is an additional hint that although it is named that way the above macro is NOT the maximum operator. Therefore, I don't think the "obvious" guess of e.g. max(a,3) = 4 is correct. Consequently, I don't know what is going on.
EDIT: I forgot to mention: I know that there are parentheses missing for the correct use. But I am specifically asked to evaluate the terms without them. Therefore I am confused since I do not know exactly how the results change and the function behave without those included.
Expanding the macro as-is, we get the following:
Original Expanded
-------- --------
c = MAX( a, 3 ); c = a>3 a : 3;
d = MAX( y, x ); d = y>x y : x;
e = MAX( ++x, 1 ); e = ++x>1 ++x : 1;
f = MAX( b, MAX (6, 7) ); f = b>MAX (6, 7) b : MAX (6, 7);
f = b>6>7 6 : 7 b : 6>7 6 : 7;
Macro expansion is just dumb text substitution - syntax, scope, precedence, associativity, values, side effects, etc., are simply not taken into account when a macro is expanded (macro expansion occurs before the source code is fed to the compiler proper).
Obviously, none of the expanded expressions above will compile. For that to happen, we need to fix the MAX macro by defining it as
#define MAX( x, y ) x>y ? x : y
Now we get the following:
Original Expanded
-------- --------
c = MAX( a, 3 ); c = a>3 ? a : 3;
d = MAX( y, x ); d = y>x ? y : x;
e = MAX( ++x, 1 ); e = ++x>1 ? ++x : 1;
f = MAX( b, MAX (6, 7) ); f = b>MAX (6, 7) ? b : MAX (6, 7);
f = b>6>7 ? 6 : 7 ? b : 6>7 ? 6 : 7;
Better. The expanded expressions above will compile, but the last two don't do anything like what you expect them to. e won't get the max of x and 1, it will either get x+2 or 1 (? introduces a sequence point, so the behavior isn't undefined). f gets ... something, can't remember the associativity of > and ?: offhand, not really willing to dig it up.
Again, macros don't take precedence and associativity into account when expanding their arguments. Imagine we write a macro CUBE that does the following:
#define CUBE(x) x * x * x
and we call it as
y = CUBE( 2 + 3 );
That expands to
y = 2 + 3 * 2 + 3 * 2 + 3;
which gives us the value 17, when we were probably expecting 125.
The right way to define the MAX macro is
#define MAX( x, y ) ((x) > (y) ? (x) : (y))
We not only parenthesize each argument, we parenthesize the entire expression. This way, precedence and associativity are preserved not only if you pass complex expressions as arguments, but also if you pass this macro as an argument to another one (as in the last example):
Original Expanded
-------- --------
c = MAX( a, 3 ); c = ((a) > (3) ? (a) : (3));
d = MAX( y, x ); d = ((y) > (x) ? (y) : (x));
e = MAX( ++x, 1 ); e = ((++x) > (1) ? (++x) : (1));
f = MAX( b, MAX (6, 7) ); f = ((b) > (MAX (6, 7)) ? (b) : (MAX (6, 7)));
f = ((b) > ((6) > (7) ? (6) : (7)) ? (b) : ((6) > (7) ? (6) : (7)));
This time, f will be evaluated like you expect it to be.
The evaluation of e is still problematic (++x can still be evaluated twice). This is a problem in general with macros that expand their arguments more than once - arguments that have side effects can be evaluated multiple times, which will lead to wrong answers or undefined behavior.
Incorrect use of ternary operator.
#define MAX(x,y) x>y x : y
Should be:
#define MAX(x,y) (x>y) ? x : y
And to allow for more complex expressions:
#define MAX(x,y) (((x)>(y)) ? (x) : (y))
You mention that you don't know what is going on. There is only way - run only the preprocessor.
What compiler are you using? Assuming gcc/clang you can -
gcc -E filename.c
This will only run the preprocessor and let you analyze what is going on.
BTW you code doesn't compile because you made a mistake with the ternary operator - should be x > y ? x : y.
I require some help please.
Using Excel 2007.
I'm not even sure this can be done.
I need to maintain efficiency and speed else the application will not be used.
I have a 'IF' statement to test for a criteria.
depending on the results want to assign one of several possible Formulas to a Variable for later use.
I then need to have the Variable to Evaluate the Formula so that the result can be stored in a second variable to build an array.
Sample Code:
If (varEmployeeName = "(All)" Or varEmployeeName = "(Multiple Items)") Then
**varArrayFormula = "Application.WorksheetFunction.SumIfs(wsSumRange, wsLookupRange, wsLookupValue)"**
Else
**varArrayFormula = "Application.WorksheetFunction.SumIfs(wsSumRange, wsLookupRange, wsLookupValue, wsLookupRange2, varEmployeeName)"**
Then Later on this code will execute:
ReDim varKPIArray(0 To varLastRow) ' creates the array size
For varCol = 10 To 13
For varRow = 8 To varLastRow
varDailyKPIHeading = ws.Cells(7, varCol).Value
Select Case varDailyKPIHeading
Case "PRODUCTIVITY"
Set wsSumRange = ThisWorkbook.Worksheets("DailyProductivity").Range("$G:$G")
Set wsLookupRange = ThisWorkbook.Worksheets("DailyProductivity").Range("$F:$F")
wsLookupValue = ws.Range("B" & varRow).Value
**varSumifValue = Application.Evaluate(varArrayFormula)**
varKPIArray(varRow - 8) = varSumifValue
Case "HOURS"
The program will then loop through several columns and rows hence why the variables are needed. I have 2 options at this point:
1) is to build an array and then I can paste the array results in the appropriate cells with a second loop.
or
2) As I am loop through each cell I Populate the formula results to the cell
Thank you everyone for you help, suggestions and ideas
Use this as your formula strings:
"SumIfs(" & wsSumRange.Address(0,0) & "," & wsLookupRange.Address(0,0) & "," & wsLookupValue & ")"
And:
"SumIfs(" & wsSumRange.Address(0,0) & "," & wsLookupRange.Address(0,0) & "," & wsLookupValue & "," & wsLookupRange2.Address(0,0) & "," & varEmployeeName & ")"
Thanks to all who gave their suggestions and Help.
Ultimately i was not able to get anything to work and ended up designing and rewriting the code.
It was hoped that there was a simply solution of applying a Formula with variables to a variable so that one did not have to keep retyping the formula and could just reference the variable that contained the formula.
I will post my solution shortly once i have the code finalized and debugged.
I've got a set of data that I've arranged in an array within mathematica. The energy value should be as shown below when I copy as LaTex format:
Theta Phi Energy(Hartree)
1.5329 & -1.5708 & -2775.20972374594 \\
1.53476 & -1.25646 & -2775.209669993 \\
1.54014 & -0.942167 & -2775.20947403366 \\
What I'm actually getting when I copy:
1.5329 & -1.5708 & -2775.21 \\
1.53476 & -1.25646 & -2775.21 \\
1.54014 & -0.942167 & -2775.21 \\
I've done multiple data sets and have gotten it to work for all but one. There seems to be a set of 100 points that keep truncating. I've attempted the following code:
PESdatatable316 = {{"Theta", "Phi", "Energy(Hartree)"}};
Do[
PESdatatable316 = Append[PESdatatable316, {th316[[i]], phi316[[i]], NumberForm[energies316[[i]], 15]}], {i, 1, 30}]
TableForm[PESdatatable316]
where:
energies316 = Flatten[{energies100,energies216},1]
The issue seems to be within 'energies100'. The values were put in with 12 digits but truncate to 6 when I call the values.
Example:
energies100[[1]]
will output:
-2775.21
'NumberForm' corrects the values within mathematica but when I copy to LaTex form it reverts back to the truncated values.
Any ideas on how I can get these values to what they're supposed to be?
A solution has been found:
the use of 'TeXForm' with a 'NumberForm' nested inside results in the correct precision and can be copied with no issues arising.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
Someone showed me the following code snippet and asked what it meant:
if (!pFCT->FBMap(| ( VBQNum - 1 ) / 8 |) & (1 << (7 - ( ( VBQNum - 1 ) % 8)))))
{
stuff
}
And I got stuck on the stand alone vertical bars. I know two together mean "or" but just one, what does that mean.
One bar by itself means "bitwise OR" (as opposed to the double bar which means "Logical OR")
1 | 1 == 1
0 | 1 == 1
1 | 0 == 1
0 | 0 == 0
true || true == true
false || true == true
01 | 10 == 11
01 || 10 == true
However, the vertical bars in your sample, as far as I can tell, are syntax errors. It looks like the author is going for "absolute value", which would use vertical bars – in writing or pseudo-code – but not in any computer language I know of.
if (!pFCT->FBMap(| ( VBQNum - 1 ) / 8 |) & (1 << (7 - ( ( VBQNum - 1 ) % 8))))) { stuff }
/* ^^^ syntax error ^^^ */
I guess whoever showed you the line in question meant absolute value
if (!pFCT->FBMap(abs( ( VBQNum - 1 ) / 8 )) & (1 << (7 - ( ( VBQNum - 1 ) % 8))))) { stuff }
/* ^^^^^^ ^^^ */
Oh! A single vertical bar means bitwise or.
Bitwise inclusive or:
The bitwise-inclusive-OR operator compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
Source.
It is a bitwise OR.
Essentially it takes the two values and ORs each of the corresponding bits in their binary representations:
10010001
01001010
--------
11011011
If either operand's bit is a 1, the answer's bit in that place is a one. If neither are 1s, the answer has a 0 there.
It is a bitwise OR operator.
Dale said he knew it meant "or" with two operands. His question is what it means when used as a unary operator. Short answer is that it doesn't mean anything in C/C++.
In some languages (like Verilog for coding hardware) this is an or-reduction, which result in a 1 if any bit is one.
So I think this is a syntax error unless the is something funny going on overloading the parentheses that I can't get my head around.
Your code is not valid C, unless put in a specific (and quite artificial) context. Operator | is a binary operator. It is a bitwise-or, as you seem to know already. By itself, it cannot be used the way it is used in your code.
If one wanted to force this code to compile as C code, one'd probably have to define FBMap as a macro. Something like
#define FBMap(x) something_else(abs(0 x 0))
thus trying to emulate the mathematical "absolute value" operator | |. Your call will expand into
pFCT->something_else(abs(0 | ( VBQNum - 1 ) / 8 | 0))
thus making the application of | operator valid.
But even after that you'd need something_else to be a function pointer in that *pFCT struct, since the call looks awfully as a C++ method call. Your question is tagged C, so the only way to make it work in C is to introduce a function pointer member into the struct.
Sorry for the very basic question. What does the & operator do in this SQL
WHERE (sc.Attributes & 1) = 0
sc is an alias for a table which contains a column attributes.
I'm trying to understand some SQL in a report and that line is making it return 0 entries. If I comment it out it works. I have limited SQL knowledge and I'm not sure what the & 1 is doing.
& is the bitwise logical and operator - It performs the operation on 2 integer values.
WHERE (sc.Attributes & 1) = 0
The above code checks to see if sc.Attributes is an even number. Which is the same as saying that the first bit is not set.
Because of the name of the column though: "Attributes", then the "1" value is probably just some flag that has some external meaning.
It is common to use 1 binary digit for each flag stored in a number for attributes. So to test for the first bit you use sc.Attributes&1, to test for the second you use sc.Attributes&2, to test for the third you use sc.Attributes&4, to test for the fourth you use sc.Attributes&8, ...
The = 0 part is testing to see if the first bit is NOT set.
Some binary examples: (== to show the result of the operation)
//Check if the first bit is set, same as sc.Attributes&1
11111111 & 00000001 == 1
11111110 & 00000001 == 0
00000001 & 00000001 == 1
//Check if the third bit is set, same as sc.Attributes&4
11111111 & 00000100 == 1
11111011 & 00000100 == 0
00000100 & 00000100 == 1
It is a bitwise logical AND operator.
It's a bitwise and.
Seeing as you tagged this as sql server, I thought I'd add something from a different angle as also ran into one of these this week.
These can hurt the performance of your queries if used in the predicate. Very easy to manufacture an example of your own. Here is the snippet from my query
WHERE
advertiserid = #advertiserid
AND (is_deleted & #dirty > 0)
WHERE
advertiserid = #advertiserid
AND (is_deleted > 0 AND #dirty > 0)
by simply defining each column with a proper value this allowed the optimizer to remove a bookmark lookup and performance stats showed a X10 performance increase.