logic

C# & operator clarification

送分小仙女□ 提交于 2019-12-04 23:37:50
I saw a couple of questions here about the diference between && and & operators in C#, but I am still confused how it is used, and what outcome results in different situations. For example I just glimpsed the following code in a project bMyBoolean = Convert.ToBoolean(nMyInt & 1); bMyBoolean = Convert.ToBoolean(nMyInt & 2); When it will result 0 and when >0? What is the logic behind this operator? What are the diferences between the operator '|'? bMyBoolean = Convert.ToBoolean(nMyInt | 1); bMyBoolean = Convert.ToBoolean(nMyInt | 2); Can we use the &&, || operators and get the same results

Insert a row between two rows in SQL

白昼怎懂夜的黑 提交于 2019-12-04 22:41:27
I have a table like this, which shows which user commented on which parent thread. ParentID CommentID UserName CommentDateTime 58 58 Vicky 2016-12-02 11:51:07.270 58 61 Billu 2016-12-02 12:35:40.220 58 62 Rakesh 2016-12-02 12:37:42.133 If suppose a comment is made to the 2nd row, a new commentid is generated which is 63 I want to write a sql query which list the rows in the below order : ParentID CommentID UserName CommentDateTime 58 58 Vicky 2016-12-02 11:51:07.270 58 61 Billu 2016-12-02 12:35:40.220 61 63 Rakesh 2016-12-02 13:37:42.133 58 62 Rakesh 2016-12-02 12:37:42.133 Could you please

Conditional statement order by frequency or compute time?

天涯浪子 提交于 2019-12-04 19:43:11
Let's say I have 100 different conditions in a IF-ELSE statement. if((boolean = methodA)){ ... } else((boolean = methodZ)){ ... } Logically, I think the least possible condition should go to the last condition(the one with methodZ) and the most frequent condition should go to ths first condition(methodA). Then I thought "what if methodA takes a lot of time compute?". methodZ would take more time than to reach even if its least frequent. Should I order the conditions by its compute time? Or order them by just their frequency? What would be a good approach to resolve this dilemma? Let's say I

Predictional Logic in Programming?

跟風遠走 提交于 2019-12-04 19:41:58
I was thinking about how in the probably distant future many people think that we wont rely on physical input (i.e. keyboard) as much because the technology that reads brain waves (which already exists to some extent) will be available. Kinda scares me....anyway, I while I was daydreaming about this, the idea came to me that: what if a programmer could implement logic in their code to accurately predict the users intentions and then carry out the intended operation with no need for human interaction. I am not looking for anything specific, I'm just a little curious as to what anyone's thoughts

Swap Three Numbers In Single Statement

隐身守侯 提交于 2019-12-04 17:37:40
问题 Is there any possiblty to swap three numbers in a single statement Eg : a = 10 b = 20 c = 30 I want values to be changed as per the following list a = 20 b = 30 c = 10 Can these values be transferred in a single line? 回答1: I found another solution for this question. You can use this in many languages like C,C++ and Java . It will work for float and long also. a=(a+b+c) - (b=c) - (c=a); 回答2: $ python >>> a, b, c = 10, 20, 30 >>> print a, b, c 10 20 30 >>> a, b, c = b, c, a >>> print a, b, c 20

Alternative to bitwise operation

筅森魡賤 提交于 2019-12-04 17:16:31
Scenario : I have say 4 check boxes and users can select those checkboxes in any combination(they also have the power to not select even a single check box). I have to store these 4 options to a single column. I think the best option is to store using binary representation option1 has the constant value 1 option2 has the constant value 2 option3 has the constant value 4 option4 has the constant value 8 So if the customer selects option2 and option4, then the value that is stored in the DB will be 2 + 8 ie: 10, if customer selects option1, option4 and option8 the value will be 1 + 4 + 8 which

R - vectorised conditional replace

↘锁芯ラ 提交于 2019-12-04 16:29:28
Hi I'm trying manipulate a list of numbers and I would like to do so without a for loop, using fast native operation in R. The pseudocode for the manipulation is : By default the starting total is 100 (for every block within zeros) From the first zero to next zero, the moment the cumulative total falls by more than 2% replace all subsequent numbers with zero. Do this far all blocks of numbers within zeros The cumulative sums resets to 100 every time For example if following were my data : d <- c(0,0,0,1,3,4,5,-1,2,3,-5,8,0,0,-2,-3,3,5,0,0,0,-1,-1,-1,-1); Results would be : 0 0 0 1 3 4 5 -1 2 3

Test if string is not equal to either of two strings

匆匆过客 提交于 2019-12-04 16:28:03
问题 I am just learning RoR so please bear with me. I am trying to write an if or statement with strings. Here is my code: <% if controller_name != "sessions" or controller_name != "registrations" %> I have tried many other ways, using parentheses and || but nothing seems to work. Maybe its because of my JS background... How can I test if a variable is not equal to string one or string two? 回答1: This is a basic logic problem: (a !=b) || (a != c) will always be true as long as b != c. Once you

Categorizing the list of array in python

耗尽温柔 提交于 2019-12-04 16:09:10
i was helping my friend to do logic algorithm in python but i haven't come with best solution yet. first of all, i have a list of array: x = array[0,1,2,3,4,3,2,3,-2,-4,-7,2,2] and he wanted to categorize the x so the output become like this: array([0,1,2,3,4]) # increasing value array([4,3,2]) #decreasing value array([2,3]) # increasing value array([3,-2,-4,-7]) #decreasing value array([-7,2]) # increasing value array([2,2]) # remain_the_same_value the rule is simple: if the value keep increasing (like example above: 0,1,2,3,4) they put in one array if the value keep decreasing (like example

python build a dynamic growing truth table

血红的双手。 提交于 2019-12-04 16:05:37
问题 My question is simple: "how to build a dynamic growing truth table in python in an elegant way?" for n=3 for p in False, True: for q in False, True: for r in False, True: print '|{0} | {1} | {2} |'.format(int(p),int(q), int(r)) for n=4 for p in False, True: for q in False, True: for r in False, True: for s in False, True: print '|{0} | {1} | {2} | {3}'.format(int(p),int(q), int(r), int(s)) I would like to have a function which takes n as a parameter and builds up the table, it is not