In one of the stored procedures in TSQL I came across the code
Alter procedure Name
#parameter1 ,
#parameter2 ,
..........
if (substring(#bitmap,1,1) & 1 = 1) or //what does this mean?
(substring(#bitmap,1,1) & 2 = 2) or
(substring(#bitmap,1,1) & 4 = 4) or
(substring(#bitmap,1,1) & 128 = 128) or
(substring(#bitmap,2,1) & 1 = 1)
[Column1] = case substring(#bitmap,1,1) & 1 when 1 then #parameter1 else [Column1] end,
[Column2] = case substring(#bitmap,1,1) & 2 when 2 then #parameter2 else [Column2] end,
[Column3] = case substring(#bitmap,1,1) & 4 when 4 then #parameter3 else [Column3] end,
[Column4] = case substring(#bitmap,1,1) & 8 when 8 then #parameter4 else [Column4] end,
[Column5] = case substring(#bitmap,1,1) & 16 when 16 then #parameter5 else [Column5] end,
[Column6] = case substring(#bitmap,1,1) & 32 when 32 then #parameter6 else [Column6] end,
[Column7] = case substring(#bitmap,1,1) & 64 when 64 then #parameter7 else [Column7]end,
[Column8] = case substring(#bitmap,1,1) & 128 when 128 then #parameter8 else [Column8] end,
[Column9] = case substring(#bitmap,2,1) & 1 when 1 then #parameter9 else [Column9] end,
[Column10] = case substring(#bitmap,2,1) & 2 when 2 then #parameter10 else [Column10] end
Googling this i found that it helps to dynamically build the procedure. But I still have few doubts.
(substring(#bitmap,1,1) & 1 = 1) or //what does this mean?
substring(#bitmap,2,1) & 1 when 1 then #parameter9 else [Column9] end,//what does this mean
case SUBSTRING(#bitmap,1,1) & 256 when 256 or
case SUBSTRING(#bitmap,1,1) & 512 when 512
Thanks
#bitmap is a bitmask which defines which of the #parameterN values is used to overwrite the (existing?) values of the ColumnN values.
The expression (value & x) when x checks whether a given bit is set in the #bitmap for x being a power of 2.
The expression CASE expression WHEN value THEN x ELSE y END is equivalent to the IF ELSE statement (or if/switch in C# etc.).
Related
Im creating a program for NIM for my Python Intro class, and am having a problem with trying to get the program to finish.
I am at a loss...
def main():
import random
#User random to generate integer between 10 and 100 for random pile size of marbles.
ballCount = random.randint(10, 100)
print("The size of the pile is: ",ballCount)
#Generate a random integer between 0 and 1, this will tell if the human or computer takes first turn.
def playerTurn(ballCount):
playerTurn = random.randint(0, 1)
if playerTurn == 0:
print("Its the computers turn...")
else:
print("Its your turn...")
#Generate a random integer between 0 and 1, to determine if the computer is smart or dumb.
computerMode = random.randint(0, 1)
def computerDumbMode(ballCount):
computerMode = random.randint(0, 1)
if computerMode == 0:
print("This computer is very dumb, no need to stress")
def computerSmartMode(ballCount):
computerMode = random.randint(0, 1)
if computerMode == 1:
print("This computer is very intelligent, beware")
#In dumb mode the computer generates random values (between 1 and n/2),
#you will use a random integer n for ballCount, when it is the computers turn.
while ballCount != 1: #This will compile untill you are left with only one marble.
if playerTurn == 0: #This will initiate computers turn.
if computerMode == 1: #This will initiate Smart Mode.
temp = random.randint(1, ballCount/2) #Pick a random number between 1, and n/2.
else: # Pick your own number of Marbles for your ballCount (n).
if ballCount - 3 > 0 and ballCount - 3 < ballCount/2:
temp = ballCount - 3
elif ballCount - 7 > 0 and ballCount - 7 < ballCount/2:
temp = ballCount - 7
elif ballCount - 15 > 0 and ballCount - 15 < ballCount/2:
temp = ballCount - 15
elif ballCount - 31 > 0 and ballCount - 31 < ballCount / 2:
temp = ballCount - 31
else:
temp = ballCount - 63
print("The computer has chosen: %d marbles." % temp) #Print the number of marbles the computer has chosen.
ballCount -= temp #Then subtract the number of marbles chosen by the computer.
else:
ballCountToPick = int(input("It is now your turn, Please pick the number of marbles in the range 1 - %d: " % int(ballCount/2))) #Reads the number of marbles to be picked by user.
while ballCountToPick < 1 or ballCountToPick > ballCount/2: #If computer reads this invalidly, it will try repeatedly.
ballCountToPick = int(input("The number you chose, is incorrect. Try again, and pick marbles in the range 1 - %d: " % int(ballCount/2)))
ballCount -= ballCountToPick #Subtract the marbles that were chosen by user.
playerTurn = (playerTurn + 1) % 2 #Changes the turn of player.
print("Now the pile is of size %d." % ballCount)
if playerTurn == 0: #Show the outcome.
print("You came, you saw, and you won... CONGRATULATIONS!!!")
else:
print("Once again... You lost and the computer wins!!!")
main()
Trying to get the program/game to run and print end result if the computer or person wins!
Your "playerTurn" variable is a function in the following line:
playerTurn = (playerTurn + 1) % 2 #Changes the turn of player.
Have a separate "playerTurn" function and "player_turn" variable will resolve your problem.
I have the following 3 examples of case expressions in postgres, which I would expect to evaluate in the same way. However the first and the third give ERROR: invalid input syntax for integer: "2017,7". The second one seems to be ok. Why is the difference?
Postgres documentation states
"A CASE expression does not evaluate any subexpressions that are not
needed to determine the result."
select case when 0 = 0 then 1 < 2
when 0 = 2 then ('2017.7')::bigint > 2000
end
(DB Fiddle)
select case when 0 = 0 then 1 < 2
when 0 = 2 then 2000 = ('2017.7'||'')::bigint
end
(DB Fiddle)
select case when (array[1,2])[1] =1 then 1 < 2
when (array[1,2])[1] = 2 then 2000 = ('2017.7'||'')::bigint
end
(DB Fiddle)
if ((status & 0x3F) == 1 ){ }..
the status is variable in swift language.
what is mean about this condition, & mean and (status & 0x3F) value return
& is the bitwise AND operator. It compares the bits of the two operands and sets the corresponding bit to 1 if it is 1 in both operands, or to 0 if either or both are 0.
So this statement:
((status & 0x3F) == 1)
is combining status with 0b111111 (the binary equivalent of 0x3F and checking if the result is exactly 1. This will only be true if the last 6 bits of status are 0b000001.
In this if:
if( (dtc24_state[2] & 0x8) == 0x8 ) {
self.haldexABCDTC24State.text = status_str + " - UNKNOWN"
self.haldexABCDTC24State.textColor = text_color
active_or_stored_dtc = true
}
dct24_state is an array of values. The value of dct24_state[2] is combined with 0x8 or 0b1000 and checked against 0x8. This is checking if the 4th bit from the right is set. Nothing else matters. If the 4th bit from the right is set, the if is true and the code block is executed.
0x3F is 111111. So, it means this:
for each bit of yourNumber in binary system presentation use and method.
This way truncates the left part of the number. and the result compares with 1.
e.g.
7777 is 1111001100001 after executing and this number converts into
100001. So the result is false.
But for 7745 (1111001000001) the result is 1. The result is true.
The rule for 'and' function: 0 & 0 = 0 ; 0 & 1 = 0; 1 & 0 = 1; 1 & 1 = 1.
I have 2 calculated fields A & B they both are calculated using Table calculation.
Now I want is to make bins using them like:
Example: My Table calculation A & B
What I want to do is:-
IF A > 0 & B > 0 Then Case 1
IF A < 0 & B < 0 Then Case 2
IF A > 0 & B < 0 Then Case 3
IF A < 0 & B > 0 Then Case 4
Thank you for your response & apologize for not attaching the Workbook.
This should be fairly simple using IF + ELSEIFs - try the formula below:
IF (A > 0 & B > 0) THEN "Case 1"
ELSEIF (A < 0 & B < 0) THEN "Case 2"
ELSEIF (A > 0 & B < 0) THEN "Case 3"
ELSEIF (A < 0 & B > 0) THEN "Case 4"
END
This should return 4 distinct values Case 1, Case 2, Case 3, Case 4 based on the input variables A and B. Hope this helps!
I'm quite new to Matlab so excuse me for the basic question.
I need to make a for-loop that repeats it's self 384 times.
So :
for i=1:384
I now need the for loop to check if 2 certain variables have the value 1 through 10, and then let them store this in a new variable with that value.
So:
if x==1
somevariable = 1
elseif x== 2
saomevariable = 2
..
..
..
elseif y = 1
someothervariable = 1
etc etc.
Is there a way to write this more efficient?
Thank you!
The first think you can do is:
if(x >= 1 && x <= 10)
somevariable=x;
end
if(y >= 1 && y <= 10)
someohtervariable=y;
end
If you could post more information about "x" and "y", perhaps your script can be further "improved".
Hope this helps.