Programming Lesson of the Day
-
Don't get me wrong ... I'm not dissing on C and saying C# is all that great. I was simply pointing out why the if statement would fail to even compile in this particular case when trying this in C#. You make a point where C#'s strong typing does make a programmer's life more cumbersome - you'd need to convert a byte array into specific types to do what you want, though there is readily available built-in libraries for that, or you could use an unsafe code block and use pointers to cast one into the other (just like you'd have done in C). Point is it would make for more coding to achieve the same thing. Though it's only in special circumstances (like your example) where this is beneficial. Nearly everywhere else it means there's less to think about (and guard against) due to the compiler checking types for you. If you find you constantly run into situations where you need to cast between types - then perhaps C# isn't the correct tool for the job and you'd be better off with C instead.
I didn't get you wrong, neither I was dissing C# - each task has its tool for the job. For a graphical interface I would most definetely use C# (now I'm stuck to VB6 due to company decisions), and for almost any program. My job involves both hardware control and computation/memory heavy algorithms so C/C++ is the tool of excellence as of now.
CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"
-
A massive recall of all existing code is being contemplated... Warning: "|" is also not the same as "||".
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
Did I sort the "have I locked my car?" effect? ;P
CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"
-
I find the strong typing of C# is more of a help than a hindrance. Back in my C days I would get into horrible tangles doing precisely what you are trying to do. An int is not a bool even though many programmers of the era would treat them alike. I still encounter data tables that have integers or even strings used as boolean values and it makes my skin crawl.
We're philosophical about power outages here. A.C. come, A.C. go.
Sometimes the assumption makes code shorter but definetely not faster (optimizars take away all the fun) or elegant. In fact I stopped using them interchangeably a while ago, now I use integers as bools only when marshalling between different conventions or storing in exchangeable data structures - it allows me to fix the size of the data field without stumbling in the differences of bool type size between compilers and languages.
CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"
-
No. Example 2 & 4 is 0 2 && is 1 (true) In that case I needed the first form as I was checking for a flag in a flag register, but mistakenly used the second form due to, well, being the most common (althought not so much in my field, which requires the management of a lot of driverless hardware).
CALL APOGEE, SAY AARDWOLF GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "Go ahead, make my day"
That depends on the language. In a strongly typed language, 1 and true aren't the same because one is a number and true is a boolean. So 2&4 is 0 and 2&&4 is 0 as well, however the difference is in side effects. SomeFunc & SomeOtherFunc will execute both functions with their side-effets, SomeFunc && SomeOtherFunc will stop executing if SomeFunc returns false. PS: I am talking about C# here which is, unlike C and C++, a strongly-typed language.