![]() |
|
This thread is locked; no one can reply to it.
![]() ![]() |
1
2
|
logical vs. binary operators |
bamccaig
Member #7,536
July 2006
![]() |
The confusion comes from modern languages like ECMAScript[1] (and evidently Ruby and others). They do work as Audric described. The operation evaluates to whatever side is last evaluated (for both && and ||, IIRC). It's a little bit of a logical fallacy, I suppose, but it does result in some fun shortcuts. Search for "last value" in this Wikipedia article. References
-- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Audric
Member #907
January 2001
|
I feel very embarrassed... By the way, I just tested with gcc on win32, and the 'booleans' produced by logical operators seem to be 4-byte size, even when not "mixed" with other data types. I tested using sizeof((1==1)) and normal optimization. |
Arthur Kalliokoski
Second in Command
February 2005
![]() |
Heh, I remember showing an instructor in the '80's how an Apple II wouldn't accept my "MOVE A TO B" command. He points out that MOVE is a COBOL keyword (yes, studying both languages at the same time). They all watch too much MSNBC... they get ideas. |
ImLeftFooted
Member #3,935
October 2003
![]() |
Technically || could return the second element for some given types. Also | can be used as a || for some given types. template<class C, class FalseT> C &operator ||&(C &a, C &b, FalseT f = false) { if(a) return a; if(b) return b; return f; }
Uh oh... whats going to happen now? int i = 0 || 5;
|
Tobias Dammers
Member #2,604
August 2002
![]() |
Audric said: By the way, I just tested with gcc on win32, and the 'booleans' produced by logical operators seem to be 4-byte size, even when not "mixed" with other data types.
It is pretty common for compilers to base booleans on the platform's native integer type. For x86, that's a 32-bit integer. --- |
|
1
2
|