Thread: is there a way to (re)define true/false to be what I want it to exact?

  1. #1
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101

    Question is there a way to (re)define true/false to be what I want it to exact?

    Hi
    true/false is vague in C,but if I want to control things with it,it would be helpful to make it work like lowlevel code
    if true=1 and false=0,different definition to use is true=0xffffffff all bits set and false=0,so you can use it as mask out things with AND
    x=x+(xd&booleanvariable) ;//AND zero=zero,AND 0xffffffff =x=x+xd
    while if true=1 and false=0,multiply with 1
    x=x+(xd*booleanvariable);
    //gets ridicoulus high results if booleanvariable is 0xfffffff
    you tell me you can C,why dont you C your own bugs?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #define FALSE 0u
    #define TRUE (~FALSE)
    TRUE becomes all bits set in whatever width of data type you end up assigning it to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Vague? ISO defines the result of boolean expressions to be 0 (false) or 1 (true). But for testing boolean values, 0 is false and non-zero, true.

    I would use
    Code:
    #define FALSE 0
    #define TRUE 1
    if you want to ensure the 0,1 values on expressions:

    Code:
    x = x + ( x & !!booleanvar );
    You can use a macro to adjust the boolean value:

    Code:
    #define BOOLEAN(x) (!!(x))
    ...
    x = x + ( x & BOOLEAN(booleanvar) );
    Last edited by flp1969; 02-20-2021 at 07:23 AM.

  4. #4
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    thanks salem,flp1969
    you tell me you can C,why dont you C your own bugs?

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Just another tip... notice that there are BOOLEAN operators and BINARY operators for logical operations... The expression above could be writen as:
    Code:
    x += ( x && boolvar );

  6. #6
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    thanks,flp1969
    I am trying something similar awsdert do,but with jumpless conditionals
    you tell me you can C,why dont you C your own bugs?

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Always a good idea to give a hand to the compiler, but be aware that it's not granted that a C jumpless code is compiled to a binary jumpless one...

    Usually, when using optimizations, the compiler will try to eliminate jumps, even with conditional ("if' or "?" operator) and loops... But it always will try to create faster code (with options as -O2 or -O3), using jumps or not.

    Here's an example:
    Code:
    int f( int x, int b )
    { return x + ( x && b ); }
    
    int g( int x, int b )
    {
      if ( x && b )
        x++;
      return x;
    }
    The g() function isn't jumpless (in C) and isn't jumpless in assembly:
    Code:
    f:                  g:
      test  edi, edi      mov   eax, edi
      setne dl            test  edi, edi
      xor   eax, eax      je    .L4
      test  esi, esi      test  esi, esi
      setne al            setne dl
      and   eax, edx      cmp   dl, 1
      add   eax, edi      sbb   eax, -1
      ret               .L4:
                          ret
    But g() is faster. If x is 0 there's a penalty for the forward conditional jump, but it is way faster than executing 5 instructions (before ret).
    if x isn't zero, the performance is equivalent for 1 less instruction than f().

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    And... sorry... I made a little mistake... To emulate && with the binary logical operator & you need to transform the values in both variables to boolean (0 or 1), like this:
    Code:
    x += ( !!x & !!b );
    This will create code exactly as
    Code:
    x += ( x && b );

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Always a good idea to give a hand to the compiler,
    Or a bad idea.

    If you write overly micro-managed "efficient" code, you typically subvert the ability of the optimiser to see through your cleverness and generate optimal code.

    Write clean simple code in the first instance, then measure the performance.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Quote Originally Posted by Salem View Post
    > Always a good idea to give a hand to the compiler,
    Or a bad idea.

    If you write overly micro-managed "efficient" code, you typically subvert the ability of the optimiser to see through your cleverness and generate optimal code.

    Write clean simple code in the first instance, then measure the performance.
    all compile options seem like a jungle,"whole program optimization", does what?glances outside the boxes(functions) to use a compatible same optimization algo,instead of optimal suited optimize algo for each one?,remember one advice against inline asm,is it interferes with the whole compiler code optimization?
    "enable fiber-safe optimization", totally cryptic too me
    seem like its time to test options like "enable parallel code generation" its only suggesting to do it,not control it todo
    you tell me you can C,why dont you C your own bugs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help.. true/false
    By salmansalman in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2008, 10:10 AM
  2. True or False
    By noob programmer in forum C Programming
    Replies: 9
    Last Post: 10-29-2008, 10:15 AM
  3. True / False
    By goran00 in forum C Programming
    Replies: 13
    Last Post: 03-14-2008, 03:26 PM
  4. 1 or 0 == true or false?
    By Discolemonade in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2005, 04:08 PM
  5. True or false
    By Eibro in forum Tech Board
    Replies: 9
    Last Post: 09-15-2002, 08:07 AM

Tags for this Thread