大多数语言包括C、C++、java和Python提供布尔类型,它们可以被设置为 错误的 或 符合事实的 . 考虑下面使用的程序 不符合逻辑(或!) 布尔运算。
null
// A C/C++ program that uses Logical Not or ! on boolean#include <stdio.h>#include <stdbool.h>int main(){ bool a = 1, b = 0; a = !a; b = !b; printf("%d%d", a, b); return 0;}// Output: 0// 1
JAVA
// A Java program that uses Logical Not or ! on boolean import java.io.*; class GFG { public static void main (String[] args) { boolean a = true , b = false ; System.out.println(!a); System.out.println(!b); } } // Output: False // True |
python
# A Python program that uses Logical Not or ! on boolean a = not True b = not False print a print b # Output: False # True |
C#
// C# program that uses Logical // Not or ! on boolean using System; class GFG { public static void Main () { bool a = true , b = false ; Console.WriteLine(!a); Console.WriteLine(!b); } } // Output: False // True // This code is contributed // by Rajput-Ji |
Javascript
<script> // A javascript program that uses Logical Not or ! on boolean var a = true , b = false ; document.write(!a+ "<br/>" ); document.write(!b); // Output: False // True // This code contributed by gauravrajput1 </script> |
上述程序的输出与预期一致,但如果我们没有使用,以下程序的输出可能与预期不一致 按位Not(或~) 接线员。
python
# A Python program that uses Bitwise Not or ~ on boolean a = True b = False print ~a print ~b |
// C/C++ program that uses Bitwise Not or ~ on boolean#include <bits/stdc++.h>using namespace std;int main(){ bool a = true, b = false; cout << ~a << endl << ~b; return 0;}
JAVA
// A Java program that uses Bitwise Not or ~ on boolean import java.io.*; class GFG { public static void main (String[] args) { boolean a = true , b = false ; System.out.println(~a); System.out.println(~b); } } |
输出:
6: error: bad operand type boolean for unary operator '~' System.out.println(~a); ^7: error: bad operand type boolean for unary operator '~' System.out.println(~b); ^2 errors
结论: “不符合逻辑!”表示布尔值,“按位not或~”表示整数。当应用整数运算符时,C/C++和python等语言会自动将布尔类型升级为整数类型。但Java没有做到这一点。 本文由 阿比特·阿加瓦尔 .如果你喜欢GeekSforgek,并且想贡献自己的力量,你也可以写一篇文章,并将文章邮寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END