Thread: bool variables

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    24

    bool variables

    I have a vector of bool vals;
    V1 = [0,1,0,1]
    V2 = [1,0,0,1]
    V3 = [1,1,1,1]
    V4 = [0,0,0,1]

    V1*V2 = [0,0,0,0]
    V1*V3 = [0,1,0,1]
    V1*V4 = [0,0,0,1]


    what is fastest way to perform the following operation in C++

    if V1*V2 = V1 then true else false?
    if V1*V3 = V1 then true else false?
    if V1*V4 = V1 then true else false?

    is there a way to do it with addition/subtraction instead of multiplication?

    when I do subtraction the -1 val become a true.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Multiplication can be done by &&. I do not know about faster or slower, but it makes more sense than multiplication where boolean values are concerned. (What does true multiplied by true mean?)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you really have to do this without multiplication? Have you actually seen that it is a bottleneck?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Generally, for boolean values, you use AND(&&) for multiplication, and XOR(!=) for addition, assuming the boolean is seen as integers modulo 2.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by Elysia View Post
    Do you really have to do this without multiplication? Have you actually seen that it is a bottleneck?
    I don't even know how to do bottleneck testing, was just wondering if someone had some insight. how can i do that?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you run your program, does it take long time to execute?
    If yes, then you can use a profiler to find out where most of the time is spent. If the time is spent on your multiplication, then you can optimize it. Otherwise it's called premature optimization and that's never a good thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Problem displaying bitmaps
    By batman123 in forum Game Programming
    Replies: 2
    Last Post: 01-09-2005, 02:01 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Can someone help me with this console app please?
    By Marcos in forum C++ Programming
    Replies: 4
    Last Post: 07-26-2003, 07:04 PM