该程序应给出以下数字的 AND.这是什么意思
This program should give the AND of the following numbers. What does this mean?
对于我C++编程课的一个作业,我得到了以下代码。 作业只是说"这个程序应该给出以下数字的 AND"想知道是否可以澄清含义。 我有一个想法,但我认为我仍然需要一些建议。 代码是故意提供的混乱,我必须清理。 这是清理:
// Question2
// This program should give the AND of the inputted numbers.
#include <iostream>
//**Needs namespace statement to directly access cin and cout without using std::
using namespace std;
//**Divided up statements that are running together for better readability
//**Used more spacing in between characters and lines to further increase readability
////void main()
//**Main function should include int as datatype; main is not typically defined as a void function
int main()
{
int i;
int k;
//**Changed spacing to properly enclose entire string
cout << "Enter 0 (false) or 1 (true) for the first value: " << endl;
cin >> i;
cout<< "Enter 0 (false) or 1 (true) for the second value: " << endl;
cin >> k;
//**Spaced out characters and separated couts by lines
//**to help with readability
cout << "AND" << endl;
cout << "kt| 0t| 1" << endl;
cout << "---t| ---t| ---" << endl;
cout << "0t| 0t| 0" << endl;
cout << "1t| 0t| 1" << endl;
if(i==1&k==1)
cout <<"Result is TRUE" << endl;
else cout << "Result is FALSE" <<endl;
//**Main function typically includes return statement of 0 to end program execution
return 0;
}
每个数字都有一个二进制表示形式。他们要求逻辑和位。查找&
运算符。
'&' 是按位的 AND,这意味着它采用两个数字的二进制表示,并将第一个数字中的每个位与第二个数字上相同位置的位进行比较。 如果两者都为 1,则输出编号中相同位置的结果位为 1,否则该位为零。 if (i&k)
会有相同的结果(假设输入始终为 0 或 1),但无论如何,您的 if 语句会比较第一位是 0 还是 1,如果两者都是 1,则返回 1。
the AND gate(output) will be true only if both inputs are true(1)
true if i==1 && k==1
false if i==0 && k==0,i==1 && k==0,i==0 && k==1.
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 通过JNI传递数据数组的最快方法是什么
- "using namespace std;"在C++的作用是什么?
- std::d eque : "insertion and deletion of elements may invalidate iterators"是什么意思?
- 该程序应给出以下数字的 AND.这是什么意思
- C++中的"and"、"or"等关键字的目的是什么?
- "Invalid operands to binary expression (ostream and void)"是什么意思,如何解决?
- "duplicate symbol _heating_unit in BangBangControlTest.o and BangBangControl.o"是什么意思?
- 在 C 语言中 &(AND) 运算符的用法是什么?