在我的Be Prepared Comp Sci 教科书中,我遇到了这个问题:
假设 x、y 和 z 是整数变量,以下三个逻辑表达式中哪一个是等价的,即对于 x、y 和 z 的所有可能值具有相等的值?
(x == y && x != z) || (x != y && x == z)
(x == y || x == z) && (x != y || x != z)
(x == y) != (x == z)
A. None of the three
B. I and II only
C. II and III only
D. I and III only
E. I, II, and III
Run Code Online (Sandbox Code Playgroud)
我选择了“B”,但弄错了。我真的认为我需要帮助理解布尔逻辑。正确答案说的是别的,但我不明白逻辑。以下是正确答案:
表达式 III …
我在Delphi中有源代码我按照http://hscripts.com/tutorials/cpp/bitwise-operators.php为按位运算符在C++ Builder中转换它,但结果是不同的
Delphi中的源代码
procedure TForm1.Button1Click(Sender: TObject)
var
tmp, dynamicINT : integer;
begin
dynamicINT := 42080;
tmp := ((dynamicINT shl 1) or (dynamicINT shr 31) and $7FFFFFFF);
Edit1.Text := IntToHex(tmp, 4);
end;
Run Code Online (Sandbox Code Playgroud)
德尔福结果:148C0正确!
C++ Builder中的源代码
void __fasctall TForm1::Button1Click(TObject *Sender)
{
int tmp = 0;
int dynamicINT = 42080;
tmp = ((dynamicINT << 1) || (dynamicINT >> 31) && 0x7FFFFFFF);
Edit1->Text = IntToHex(tmp, 4);
}
Run Code Online (Sandbox Code Playgroud)
C++ Builder结果:0001???
转换有什么问题?
我正在使用C++ Builder 6和Delphi 7
如何避免编写疯狂的combineLatest语句来计算简单的布尔逻辑表达式?
例如。这个简单的表达式几乎不适合 stackoverflow 代码控件,如果你不小心重新排序参数,你将很难调试!
this.showPlayButton = combineLatest(this.playPending, this.isReady, this.showOverlay)
.pipe(
map(([playPending, isReady, showOverlay]) => isReady && !playPending && showOverlay),
distinctUntilChanged();
Run Code Online (Sandbox Code Playgroud) |之间有什么重大区别吗?和+会影响代码的长期性能?或者都是O(1)?我正在使用的代码是这样的:
uint64_t dostuff(uint64_t a,uint64_t b){
// the max values of the inputs are 2^32 - 1
// lots of stuff involving boolean operators
// that have no way of being substituted by
// arithmetic operators
return (a << 32) + b;
//or
return (a << 32) | b;
}
Run Code Online (Sandbox Code Playgroud)
代码将被多次使用,所以我想尽可能加快速度.
从一系列TRUE和falses,我想创建一个返回TRUE的函数,无论n1序列中某处是否存在至少一系列TRUE .这是这个功能:
fun_1 = function(TFvec, n1){
nbT = 0
solution = -1
for (i in 1:length(x)){
if (x[i]){
nbT = nbT + 1
if (nbT == n1){
return(T)
break
}
} else {
nbT = 0
}
}
return (F)
}
Run Code Online (Sandbox Code Playgroud)
测试:
x = c(T,F,T,T,F,F,T,T,T,F,F,T,F,F)
fun_1(x,3) # TRUE
fun_1(x,4) # FALSE
Run Code Online (Sandbox Code Playgroud)
然后,我需要一个返回TRUE的函数,如果在给定的列表中布尔向量,则n1至少有一系列由至少两个系列(每侧一个)包裹的TRUE n2.这里的功能:
fun_2 = function(TFvec, n1, n2){
if (n2 == 0){
fun_1(TFvec, n2)
}
nbFB = 0
nbFA = 0
nbT = 0
solution …Run Code Online (Sandbox Code Playgroud) 我读了一些剧本,似乎很难理解.希望有人能解释为什么第一个:
public static bool ContainsDestroyWholeRowColumn(BonusType bt)
{
return (bt & BonusType.DestroyWholeRowColumn)
== BonusType.DestroyWholeRowColumn;
}
Run Code Online (Sandbox Code Playgroud)
为什么不写bt.Equal(BonusType.DestroyWholeRowColumn)或bt == BonusType.DestroyWhoeRowColumn?第二:
public bool IsSameType(Shape otherShape)
{
if (otherShape == null || !(otherShape is Shape))// check otherShape is not null and it is Shape
throw new ArgumentException("otherShape");
return string.Compare(this.Type, (otherShape as Shape).Type) == 0;
}
Run Code Online (Sandbox Code Playgroud)
如果输入法不是正确的类型.我认为它会立即警觉,为什么他们还需要检查对象的类型最后:
//if we are in the middle of the calculations/loops
//and we have less than 3 matches, return a random one
if(row >= Constants.Rows / 2 …Run Code Online (Sandbox Code Playgroud) 在Python 中使用单词and与&符号的逻辑或性能有什么不同吗?
a [8]是一系列布尔值.我试图计算一个布尔表达式,如下所示.
bool a[8];
.....
bool result=a[0]*(a[3]+a[6]*(a[4]+a[7]*a[5]))+a[1]*(a[6]*a[3]+a[4]+a[7]*a[5])+a[2]*(a[5]+a[7]*(a[4]+a[6]*a[3]));
Run Code Online (Sandbox Code Playgroud)
表达很长,但概念上并不复杂.唯一的错误是,当我用&&替换表达式中的*(在两种情况下我都期望逻辑AND)或用||替换+时,结果似乎不同 (期待逻辑OR).
我不确定哪些是正确的,布尔运算符或乘法,加法等的数学符号.更糟糕的是,没有给出任何错误,即编译器对两者都满意.哪一个更安全,更正确?
在Ruby case条件中,我看不出布尔运算符AND和OR之间的实际区别.
例如,我想让用户输入句子:
sudo make me a sandwich
Run Code Online (Sandbox Code Playgroud)
案件条件开始如下:
case user_selection
when /sudo/ && /sandwich/
Run Code Online (Sandbox Code Playgroud)
但是,如果用户输入:
make me a sandwich
Run Code Online (Sandbox Code Playgroud)
条件将是满意的.
在这种情况下我的方法是重新排序条件:
case user_selection
when /sandwich/ && /sudo/
Run Code Online (Sandbox Code Playgroud)
但是,预先假定每次用户想要使用"sudo"时,他们会在响应中包含字符串"sandwich".但是,这在功能上与此无异:
case user_selection
when /sudo/
Run Code Online (Sandbox Code Playgroud)
我查找了Ruby条件的布尔运算符,但没有找到满意的答案.
我们假设我们有以下昂贵的功能:
bool ExpensiveOp1() { ... }
bool ExpensiveOp2() { ... }
bool ExpensiveOp3() { ... }
Run Code Online (Sandbox Code Playgroud)
另外,为了简单起见,假设它们都没有副作用.
我知道C#可以短路if ExpensiveOp1或ExpensiveOp2返回false以下表达式:
return ExpensiveOp1() && ExpensiveOp2() && ExpensiveOp3();
Run Code Online (Sandbox Code Playgroud)
但是,如果我以这种方式编写代码,编译器是否足够智能(缺少更好的术语)内联函数调用并利用短路?
var x = ExpensiveOp1();
var y = ExpensiveOp2();
var z = ExpensiveOp3();
return x && y && z;
Run Code Online (Sandbox Code Playgroud)