Jump to content

  • Log In with Google      Sign In   
  • Create Account

My OLD Syntax

  • You cannot reply to this topic
83 replies to this topic

#61 samoth   Crossbones+   -  Reputation: 4792

Like
0Likes
Like

Posted 20 August 2013 - 05:17 AM

Unfortunately, C/C++ are a little flexible in their definition of what constitutes a boolean value...

 

Well, as usual, the C++ committee took great care to avoid actually saying anything while saying a lot of things, but I think you can still deduct the de-facto definition from what they say. It's amazing how you can write 1,300 pages without laying something very simple down in a clear, understandable, non-ambiguous manner. biggrin.png

 

Values of type bool are either true or false.

    and

A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

 

Great. Very nice, except we don't know what true and false are. This isn't mentioned anywhere within those 1,300 pages. For all we know, false could be 354357 and true could be 77342.

 

However, in combination with this:

 

A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

 

one can pretty much safely presume that false == 0 and true == 1, even though again it doesn't explicitly say so. Converting to bool and converting from bool converts to/from 0 and 1, respectively.

 

Of course, given this wording, it is still technically allowable to have different values and add extra code for conversion (similarly, it is perfectly allowable for a null pointer to be something different from zero, even though the literal zero explicitly converts to the null pointer).

 

But even though technically allowable, it's not something that really makes sense. The values 0 and 1 are as good as any other values, and they do not require extra code for a conversion that has no benefit.

 

Further, it is explicitly allowable to store a bool in a bit-field, and in this case no "implementation specific" value that isn't exactly 1 would work for true. Neither would (given only two possible values in a single bit) any other value but 0 work for false, since 1 is already taken.


Edited by samoth, 20 August 2013 - 05:19 AM.


Sponsor:

#62 swiftcoder   Senior Moderators   -  Reputation: 10090

Like
0Likes
Like

Posted 20 August 2013 - 06:34 AM

one can pretty much safely presume that false == 0 and true == 1, even though again it doesn't explicitly say so.

Fair enough.

However, the code sample I was commenting on didn't indicate that the values being operated on were actually booleans. It's not uncommon for legacy code to perform boolean operations on integers, for example.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#63 Servant of the Lord   Crossbones+   -  Reputation: 19685

Like
3Likes
Like

Posted 20 August 2013 - 10:59 AM

When I was a newer programmer, it felt unintuitive to me that -1 == true. I just naturally assumed positive numbers were true, and zero + negative numbers were false. smile.png


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#64 Bregma   Crossbones+   -  Reputation: 5188

Like
3Likes
Like

Posted 20 August 2013 - 11:06 AM

I've used machines where true == ~false (ie. ~0 == -1 in two's complement) because that was the most efficient way to encode it in the machine instructions.  A conversion to the integral value 1 would take extra instructions, but it's a corner case.  Logical operations are performed on booleans far far more often than arithmetic operations are.


Stephen M. Webb
Professional Free Software Developer

#65 Sik_the_hedgehog   Crossbones+   -  Reputation: 1759

Like
0Likes
Like

Posted 20 August 2013 - 03:52 PM

 


For the record, I know at least one language where it is indeed the case (it only pays attention to the lowest bit instead of the whole value).

 

But it wouldn't be bitwise anymore, then, right?

 

It makes AND, OR and XOR behave as both logical and bitwise, since technically a bitwise operation will still affect the lowest bit exactly the same way. It gets rid of the risk of mixing both variants by simply not having them (the bitwise variant now works for both cases, so the logical one is ditched).

 

EDIT: actually NOT too. No more need to have separate NOT and complement operators either.


Edited by Sik_the_hedgehog, 20 August 2013 - 03:55 PM.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#66 wodinoneeye   Members   -  Reputation: 834

Like
0Likes
Like

Posted 25 August 2013 - 07:12 PM

 

if (x & y & funcz(a)) {
// do shirt here
}
That conditional almost certainly doesn't do what you meant it to do tongue.png

Hint: bitwise & is not the same as logical &&.

 

The conditional wasnt meant to do anything but be some arbitrary condition for a formating  example (so it did exactly what I meant it to do )

 

I never use Bitwise operators on single value Boolean variables or conditional clauses ,  But have regularly used bit-wise operators in my coding as a way to compact data in games (single bit flags in sets of flags for objects) . I also have judiciously used the pack settings (on compilers that actually implement them)....

 

How large do the C/C++ compliles make a single  Boolean variable these days   32 bits, 8 at best?    Blasting needlessly oversized object structures through a network  wasnt conducive to efficiency (including possibly Page Fault stallings in general)

 

 

 

Some might say that is a time for size tradeoff, but dont the out-of-order processing of instructions cancel most fo that these days? 

 

NOTE - I also did exclusively X86 coding so dont read me the usual stuff about endians and compiler differenced in packing

 

Since someone else brought it up I also #define'd my own  constants TRUE 1  FALSE 0   simply to make that clear and also to make the values use stand out in code  (though in bitwise masking I often used hex  0x0001 and such forrmats  to make what was going on clear also)


Edited by wodinoneeye, 25 August 2013 - 07:32 PM.

--------------------------------------------Ratings are Opinion, not Fact

#67 Solid_Spy   Members   -  Reputation: 420

Like
0Likes
Like

Posted 27 August 2013 - 08:12 PM

Curly brace arguments. Keeping programmers busy since 1969. tongue.png

 

Besides, the only correct brace style is the one I use.

Which one do you use eh?


Edited by Solid_Spy, 27 August 2013 - 08:13 PM.


#68 swiftcoder   Senior Moderators   -  Reputation: 10090

Like
1Likes
Like

Posted 27 August 2013 - 08:23 PM


Which one do you use eh?

Quantum braces.

 

They are of indeterminate style until observed, at which point they concretely represent a randomly-selected style.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#69 Sik_the_hedgehog   Crossbones+   -  Reputation: 1759

Like
1Likes
Like

Posted 27 August 2013 - 09:50 PM

Schrödinger's braces?


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#70 Bacterius   Crossbones+   -  Reputation: 8950

Like
1Likes
Like

Posted 27 August 2013 - 10:00 PM

Schrödinger's braces?

 

They look like this: \(\langle \text{code} \rangle \)


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#71 unbird   Crossbones+   -  Reputation: 5115

Like
1Likes
Like

Posted 28 August 2013 - 04:30 AM

IIRC they help greatly against Heisenbugs.



#72 Paradigm Shifter   Crossbones+   -  Reputation: 5381

Like
1Likes
Like

Posted 28 August 2013 - 04:34 AM

Are you certain about that?


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#73 unbird   Crossbones+   -  Reputation: 5115

Like
1Likes
Like

Posted 28 August 2013 - 05:15 AM

laugh.png

Touché!



#74 wodinoneeye   Members   -  Reputation: 834

Like
1Likes
Like

Posted 29 August 2013 - 12:07 AM

Thats why having indentation patterns and blank (whitespace) padding stuff  makes the actual visual recognition of the braces/parens/whatever lessimportant (even more so as you get older and 8hours staring at a computer screen starts making your eyballs disolve)


--------------------------------------------Ratings are Opinion, not Fact

#75 unexpectedly   Members   -  Reputation: 120

Like
0Likes
Like

Posted 12 September 2013 - 01:43 PM

Curly brace arguments. Keeping programmers busy since 1969. tongue.png

 

Besides, the only correct brace style is the one I use.

 

HA! Until the Director of Software Engineering enforces his own personally preferred ideology upon his subjects. Sadly, dx'ing indexes stuck with me. (idx, jdx, kdx)

 

 

 

I only code on mechanical keyboards. So addicted to them, I bought them for every high-use workstation at my business.

 

if (keyboard_check(vk_left)) {
     x -= 5;
                             }

 

* snort *  <--- I can feel aforementioned Director of Software Engineering twitching already...

 

if 
(
      keyboard_check
      (
          vk_left
      )
) 
{
        x -= 5;              
}

 

* sigh * that makes ME twitch. :(  The first time OOP was choked down my throat, that's how those people coded. And were paid. Well paid.

 

Also, what sane language that allows bool to int casting doesn't represent the bool as 1 or 0?

 

Probably PHP. It has buried me with it's too-easy casting before.

 

Great. Very nice, except we don't know what true and false are. This isn't mentioned anywhere within those 1,300 pages. For all we know, false could be 354357 and true could be 77342.

 

Two things I miss the most from PHP: === operator and all variables needing to start with $. The $ thing makes it easier for me to see my own variables in the other mess of logic. And the === identity is true only if value and type match. So you can do a ===0 or !==0 and not really worry about accidental 1/0 true false problems.

 

I'm coming back to C (and sadly C++) from my too many years of PHP hacking web apps for my business. The C++ is for embedded things and we don't get any MSVC help, so many of my code searches are about doing things the hard way, coding it by yourself, and not relying on a framework for a crutch.

 

Thanks for the lolz!

:D Chris, the FNG



#76 wintertime   Members   -  Reputation: 1727

Like
0Likes
Like

Posted 13 September 2013 - 02:48 PM

I bet there are better languages than PHP where you could use $. Even gcc allows it as an Extension.

Ever read this? http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/ biggrin.png



#77 unexpectedly   Members   -  Reputation: 120

Like
0Likes
Like

Posted 13 September 2013 - 04:07 PM

That's awesome. :) And a great read. It reminds me of my expectations: php is a chainsaw to get the job done and I don't really expect much from it. I expect more from C and C++ and they demand more of my thought to work correctly. (so, I'm spoiled by php)

 

I really like that dude said to go learn python. Always have been meaning to. Seeing the reference and reading some led to my big chuckle for the day:

Go learn Lisp. I hear people who know everything really like Lisp.

 



#78 Sik_the_hedgehog   Crossbones+   -  Reputation: 1759

Like
0Likes
Like

Posted 13 September 2013 - 11:34 PM


Two things I miss the most from PHP: === operator and all variables needing to start with $. The $ thing makes it easier for me to see my own variables in the other mess of logic. And the === identity is true only if value and type match. So you can do a ===0 or !==0 and not really worry about accidental 1/0 true false problems.

By general rule, if a language has === and !==, that means somebody got the design of the language horribly wrong with == and != (I can see doing implicit casting with integers and/or floats, but as soon as any other type combinations are involved comparisons should always return false).

 

Also the $ thing has an advantage you didn't mention: they're guaranteed to not clash with keywords, because they never start with $. That's extremely useful in the long run, albeit it looks somewhat hackish. Shame PHP only does it for variables and not for all identifiers.


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#79 ill   Members   -  Reputation: 320

Like
0Likes
Like

Posted 14 September 2013 - 07:26 PM

That's funny.  I consider that a great style.  Also this is the style at Microsoft, and Epic.

 

I only switched to having the opening curly brace on the same line because I wanted to be consistent with others on a team I was on.  Overall I think putting it on the next line is the most flexible and doesn't require weird special cases like when you start indenting the parameters list.



#80 TheChubu   Crossbones+   -  Reputation: 4425

Like
1Likes
Like

Posted 14 September 2013 - 09:41 PM

Once I saw a random poll on a blog that had almost 2000 votes, it had like 10 different styles listed, Allman style was winning by 70% or so.

 

EDIT: Found it http://www.terminally-incoherent.com/blog/2009/04/10/the-only-correct-indent-style/


Edited by TheChubu, 14 September 2013 - 09:45 PM.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator






PARTNERS