| | |
Bitwise AND
![]() |
•
•
Join Date: Sep 2004
Posts: 2
Reputation:
Solved Threads: 0
This is giving me a headache...can someone please tell me why these statements ARE NOT equivalent?
Statement 1.)
if(running_result && shell[8 * (x - 1) + (y - 1)].input_val)
running_result = true;
else
running_result = false;
Statement 2.)
running_result &= shell[8 * (x - 1) + (y - 1)].input_val;
-------------------------
The first statement works; the second does NOT. The second always evaluates to false. Why? Both of these vars are bools.
Statement 1.)
if(running_result && shell[8 * (x - 1) + (y - 1)].input_val)
running_result = true;
else
running_result = false;
Statement 2.)
running_result &= shell[8 * (x - 1) + (y - 1)].input_val;
-------------------------
The first statement works; the second does NOT. The second always evaluates to false. Why? Both of these vars are bools.
•
•
•
•
Originally Posted by iamhe
This is giving me a headache...can someone please tell me why these statements ARE NOT equivalent?
-------------------------
The first statement works; the second does NOT. The second always evaluates to false. Why? Both of these vars are bools.
C++ Syntax (Toggle Plain Text)
#include <iostream> int main() { bool a, b; //Statement 1.) a = false; b = false; std::cout << "a = " << a << ", b = " << b; if ( a && b ) a = true; else a = false; std::cout << ": a = " << a << std::endl; a = false; b = true; std::cout << "a = " << a << ", b = " << b; if ( a && b ) a = true; else a = false; std::cout << ": a = " << a << std::endl; a = true; b = false; std::cout << "a = " << a << ", b = " << b; if ( a && b ) a = true; else a = false; std::cout << ": a = " << a << std::endl; a = true; b = true; std::cout << "a = " << a << ", b = " << b; if ( a && b ) a = true; else a = false; std::cout << ": a = " << a << std::endl; std::cout << std::endl; //Statement 2.) a = false; b = false; std::cout << "a = " << a << ", b = " << b; a &= b; std::cout << ": a = " << a << std::endl; a = false; b = true; std::cout << "a = " << a << ", b = " << b; a &= b; std::cout << ": a = " << a << std::endl; a = true; b = false; std::cout << "a = " << a << ", b = " << b; a &= b; std::cout << ": a = " << a << std::endl; a = true; b = true; std::cout << "a = " << a << ", b = " << b; a &= b; std::cout << ": a = " << a << std::endl; std::cout << std::endl; return 0; } /* my output a = 0, b = 0: a = 0 a = 0, b = 1: a = 0 a = 1, b = 0: a = 0 a = 1, b = 1: a = 1 a = 0, b = 0: a = 0 a = 0, b = 1: a = 0 a = 1, b = 0: a = 0 a = 1, b = 1: a = 1 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
the first statements uses logical and (&&) the second uses bitwise and (&), so you might try using '&&=' in the second statement.
The difference might be subtle; say the input_val was '2'. In the first statement the running result would be true and in the second statement it would be 0 because TRUE (1) & 2 == 0, but (1 && 2) is true.
The difference might be subtle; say the input_val was '2'. In the first statement the running result would be true and in the second statement it would be 0 because TRUE (1) & 2 == 0, but (1 && 2) is true.
•
•
•
•
Originally Posted by Chainsaw
the first statements uses logical and (&&) the second uses bitwise and (&), so you might try using '&&=' in the second statement.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
•
•
Originally Posted by Chainsaw
In any case, & and && are not the same.
•
•
•
•
Originally Posted by iamhe
Why? Both of these vars are bools.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Sep 2004
Posts: 2
Reputation:
Solved Threads: 0
I am so excited because I finished my program.
Chainsaw is right, any nonzero value is true in a bool, so a bitwise AND wouldn't necessarily evaluate to true... and I shouldn't have tried using a bitwise op for boolean algebra.
(Incidentally I settled on):
running_result = running_result && shell[8 * (x - 1) + (y - 1)].input_val;
Not too stylish, but it served its purpose.
Chainsaw is right, any nonzero value is true in a bool, so a bitwise AND wouldn't necessarily evaluate to true... and I shouldn't have tried using a bitwise op for boolean algebra.
(Incidentally I settled on):
running_result = running_result && shell[8 * (x - 1) + (y - 1)].input_val;
Not too stylish, but it served its purpose.
![]() |
Similar Threads
- Help with C bitwise code (C)
- Problems with bitwise operators (C)
- bitwise operations and masking (C)
- Bitwise operators (C)
Other Threads in the C++ Forum
- Previous Thread: Refresher information for C++
- Next Thread: why i have to press "enter" twice before getline can read the string...
| Thread Tools | Search this Thread |
api application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output parameter pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets






