Logic AND between some strings

Discussion in 'C++' started by yagru_alvarez, Mar 7, 2011.

  1. I have some strings, all of them such as the following type: 0111001

    I want to make a LOGIC AND between all of them. How can I perform that
    in C++. Thanks.
    yagru_alvarez, Mar 7, 2011
    #1
    1. Advertising

  2. On 3/7/2011 10:14 AM, yagru_alvarez wrote:
    > I have some strings, all of them such as the following type: 0111001


    The "0111001" is not really a *type* (in C++ sense), is it? You
    probably mean that they all had a particular *pattern* (i.e. consisted
    of the symbols '0' and '1'), yes?

    > I want to make a LOGIC AND between all of them. How can I perform that
    > in C++. Thanks.


    What does it mean, in your words, to perform "a LOGIC AND between all"
    strings in a set? For example, what would be the result of a "LOGICAL
    AND between" the strings "01" and "101"?

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 7, 2011
    #2
    1. Advertising

  3. On Mar 7, 9:23 am, Victor Bazarov <> wrote:
    > On 3/7/2011 10:14 AM, yagru_alvarez wrote:
    >
    > > I have some strings, all of them such as the following type: 0111001

    >
    > The "0111001" is not really a *type* (in C++ sense), is it?  You
    > probably mean that they all had a particular *pattern* (i.e. consisted
    > of the symbols '0' and '1'), yes?
    >
    > > I want to make a LOGIC AND between all of them. How can I perform that
    > > in C++. Thanks.

    >
    > What does it mean, in your words, to perform "a LOGIC AND between all"
    > strings in a set?  For example, what would be the result of a "LOGICAL
    > AND between" the strings "01" and "101"?
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    Ok, You are right. The strings, have a particular pattern consisted of
    symbos '0' and '1'. The length of strings is the same for all. I want
    to make a Logical AND with all this strings. Thanks
    yagru_alvarez, Mar 7, 2011
    #3
  4. yagru_alvarez

    Fred Zwarts Guest

    "yagru_alvarez" <> wrote in message
    news:
    > I have some strings, all of them such as the following type: 0111001
    >
    > I want to make a LOGIC AND between all of them. How can I perform that
    > in C++. Thanks.


    What do you mean with "strings"? Where do you have them? In a file?
    The easiest way is to read such types in an unsigned integer type and to use the & (logical and) operator.
    But as this is so evident, I have the impression that you want something else.
    Please, specify your problem a bit more clear.
    Fred Zwarts, Mar 7, 2011
    #4
  5. yagru_alvarez

    Fred Zwarts Guest

    "Fred Zwarts" <> wrote in message
    news:il31an$gli$
    > "yagru_alvarez" <> wrote in message
    > news:
    >> I have some strings, all of them such as the following type: 0111001
    >>
    >> I want to make a LOGIC AND between all of them. How can I perform
    >> that in C++. Thanks.

    >
    > What do you mean with "strings"? Where do you have them? In a file?
    > The easiest way is to read such types in an unsigned integer type and
    > to use the & (logical and) operator.


    This is the bit wise and operator.

    > But as this is so evident, I have the impression that you want
    > something else.
    > Please, specify your problem a bit more clear.
    Fred Zwarts, Mar 7, 2011
    #5
  6. yagru_alvarez

    crea Guest

    yagru_alvarez wrote:
    > I have some strings, all of them such as the following type: 0111001
    >
    > I want to make a LOGIC AND between all of them. How can I perform that
    > in C++. Thanks.


    Here you go:
    string StrAND(char* s1, char* s2)
    {
    char buffer[100];
    string ret;
    int a, b, c;
    a = atoi( s1 );
    b = atoi( s2 );
    c = (a & b);

    itoa(c, buffer, 10);
    ret = buffer;

    return ret;
    }

    :).
    crea, Mar 7, 2011
    #6
  7. On 3/7/2011 10:31 AM, yagru_alvarez wrote:
    > On Mar 7, 9:23 am, Victor Bazarov<> wrote:
    >> On 3/7/2011 10:14 AM, yagru_alvarez wrote:
    >>
    >>> I have some strings, all of them such as the following type: 0111001

    >>
    >> The "0111001" is not really a *type* (in C++ sense), is it? You
    >> probably mean that they all had a particular *pattern* (i.e. consisted
    >> of the symbols '0' and '1'), yes?
    >>
    >>> I want to make a LOGIC AND between all of them. How can I perform that
    >>> in C++. Thanks.

    >>
    >> What does it mean, in your words, to perform "a LOGIC AND between all"
    >> strings in a set? For example, what would be the result of a "LOGICAL
    >> AND between" the strings "01" and "101"?
    >>
    >> V
    >> --
    >> I do not respond to top-posted replies, please don't ask

    >
    > Ok, You are right. The strings, have a particular pattern consisted of
    > symbos '0' and '1'. The length of strings is the same for all. I want
    > to make a Logical AND with all this strings. Thanks


    You can use 'std::bitset' to convert your strings into longs and then
    use bitwise AND on all of them until you got all or until you reach 0,
    then print out the result. If using standard containers is not an
    option in your homework, then you need to write a function that would
    take two strings as arguments and return a string. The function will
    perform your logical and on the symbols of those strings (throw an
    exception if they have different lengths). You can calculate the result
    in a loop. Assign your first string to the result, then for all strings
    from 1 to the end, do the operation between the [intermediate] result
    and the i-th string (calling your function), assign the return value to
    the [intermediate] result string. Check all zeros and bail early if true.

    Or did you expect I'd write the code for you? FAQ 5.2.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 7, 2011
    #7
  8. yagru_alvarez

    crea Guest

    Paavo Helde wrote:
    > "crea" <> wrote in
    > news:qO7dp.24381$2:
    >
    >> yagru_alvarez wrote:
    >>> I have some strings, all of them such as the following type: 0111001
    >>>
    >>> I want to make a LOGIC AND between all of them. How can I perform
    >>> that in C++. Thanks.

    >>
    >> Here you go:
    >> string StrAND(char* s1, char* s2)

    >
    > 'string' is not defined, probably you mean std::string.


    yes

    >
    > One should use const char* instead of char*.
    >
    > Why the strange interface, why not use const std::string& instead of
    > const char*?


    Looked to be faster/ easier to code.

    >
    >> {
    >> char buffer[100];
    >> string ret;

    >
    > not needed
    >
    >> int a, b, c;

    >
    > no need to predeclare variables in C++.


    ?. variables needs to be declared.

    >
    >> a = atoi( s1 );

    >
    > How do you know the result fits in int? atoi() provides no error
    > check.


    Well he said it was fixed size strings, so I assumed that they are not very
    long. In his exaple they were like 100110. Obviously this function is for
    short values. If he knows all are short, then he could use this. Its faster
    to code than going bit by bit. Time is money .. :).

    >
    >> b = atoi( s2 );
    >> c = (a & b);

    >
    > There are coding guidelines which suggest to use bitwise operators
    > only on unsigned types.


    Yes to be exact, although it does not make any difference in this example
    because we know that they are "1001010".

    >
    >>
    >> itoa(c, buffer, 10);

    >
    > No such function in standard C or C++; this is a compiler extension
    > and non-portable.


    he can use his compilers version, whatever he has. I assume he knows what
    itoa does.
    crea, Mar 7, 2011
    #8
  9. yagru_alvarez

    crea Guest

    Paavo Helde wrote:
    > "crea" <> wrote in
    > news:Iz9dp.103060$2:
    >
    >> Paavo Helde wrote:
    >>> "crea" <> wrote in
    >>> news:qO7dp.24381$2:
    >>>
    >>>> yagru_alvarez wrote:
    >>>>> I have some strings, all of them such as the following type:
    >>>>> 0111001
    >>>>>
    >>>>> I want to make a LOGIC AND between all of them. How can I perform
    >>>>> that in C++. Thanks.
    >>>>
    >>>> Here you go:
    >>>> string StrAND(char* s1, char* s2)
    >>>
    >>> 'string' is not defined, probably you mean std::string.

    >>
    >> yes
    >>
    >>>
    >>> One should use const char* instead of char*.
    >>>
    >>> Why the strange interface, why not use const std::string& instead of
    >>> const char*?

    >>
    >> Looked to be faster/ easier to code.
    >>
    >>>
    >>>> {
    >>>> char buffer[100];
    >>>> string ret;
    >>>
    >>> not needed
    >>>
    >>>> int a, b, c;
    >>>
    >>> no need to predeclare variables in C++.

    >>
    >> ?. variables needs to be declared.

    >
    > But not in the beginning of the function.
    >
    >
    >>>
    >>>> a = atoi( s1 );
    >>>
    >>> How do you know the result fits in int? atoi() provides no error
    >>> check.

    >>
    >> Well he said it was fixed size strings, so I assumed that they are
    >> not very long. In his exaple they were like 100110. Obviously this
    >> function is for short values. If he knows all are short, then he
    >> could use this. Its faster to code than going bit by bit. Time is
    >> money .. :).

    >
    > Without error checking this function will silently give wrong results
    > some day. Always check for errors!
    >
    > And last, this function still does not do what he wants. When applied
    > to "0111001" and "11000", it yields "8344". I am quite sure this is
    > not the expected result.


    oh, it needs a for-loop to go every bit individually... but similar logic
    anyway
    crea, Mar 7, 2011
    #9
  10. yagru_alvarez

    James Kanze Guest

    On Mar 7, 10:03 pm, Pete Becker <> wrote:
    > On 2011-03-07 16:53:50 -0500, crea said:


    [...]
    > >> And last, this function still does not do what he wants. When applied
    > >> to "0111001" and "11000", it yields "8344". I am quite sure this is
    > >> not the expected result.


    > > oh, it needs a for-loop to go every bit individually... but similar
    > > logic anyway


    > No, it just needs to a text string that represents the integer value in
    > binary. Read about strtoul.


    And one that will output the integer value in binary as well.
    That's a little harder:).

    > You're right that looping through the
    > individual characters is a better approach; but that's not "similar
    > logic" at all.


    If you're measuring programmer performance, the maximum string
    length is known, and is reasonably small, something using
    std::bitset<N> is bound to be the simplest to write; it's also
    the only solution I know that will handle outputting binary
    automatically. Something like:

    std::string operator&(std::string const& lhs,
    std::string const& rhs)
    {
    return (bitset<maxBits>(lhs) &
    bitset<maxBits>(rhs)).to_string();
    }

    (A simple one-liner.) I rather suspect that iterating through
    the two strings would be faster, however. There's an awful lot
    going on behind the scenes in the above.

    --
    James Kanze
    James Kanze, Mar 7, 2011
    #10
  11. yagru_alvarez

    Marc Guest

    James Kanze wrote:

    > If you're measuring programmer performance, the maximum string
    > length is known, and is reasonably small, something using
    > std::bitset<N> is bound to be the simplest to write; it's also
    > the only solution I know that will handle outputting binary
    > automatically. Something like:
    >
    > std::string operator&(std::string const& lhs,
    > std::string const& rhs)
    > {
    > return (bitset<maxBits>(lhs) &
    > bitset<maxBits>(rhs)).to_string();
    > }
    >
    > (A simple one-liner.) I rather suspect that iterating through
    > the two strings would be faster, however. There's an awful lot
    > going on behind the scenes in the above.


    std::string s;
    s.reserve(lhs.size());
    std::transform(lhs.begin(),lhs.end(),rhs.begin(),std::back_inserter(s),std::min<char>);
    return s;

    That's hard to write as a one-liner :-(
    Marc, Mar 8, 2011
    #11
  12. yagru_alvarez

    Paul Guest

    "Pete Becker" <> wrote in message
    news:2011030808444315377-pete@versatilecodingcom...
    > On 2011-03-07 17:48:08 -0500, James Kanze said:
    >
    >> On Mar 7, 10:03 pm, Pete Becker <> wrote:
    >>> On 2011-03-07 16:53:50 -0500, crea said:

    >>
    >> [...]
    >>
    >>> No, it just needs to a text string that represents the integer value in
    >>> binary. Read about strtoul.

    >>
    >> And one that will output the integer value in binary as well.
    >> That's a little harder:).
    >>
    >>

    >
    > Urk. strtoul goes the wrong way, of course.
    >

    #include <iostream>

    const int STR_LEN=8;
    typedef char bin_str[STR_LEN];

    int main()
    {
    bin_str str1 = "1100110";
    bin_str str2 = "1000011";
    bin_str str3 = "0000000";


    for(int x=0; x<STR_LEN; x++){
    if(str1[x]=='1' && str1[x]==str2[x])
    str3[x]=str1[x];
    }

    std::cout<< str3;

    }
    Paul, Mar 8, 2011
    #12
  13. yagru_alvarez

    Paul Guest

    "Leigh Johnston" <> wrote in message
    news:...
    > On 08/03/2011 22:23, Leigh Johnston wrote:
    >> On 08/03/2011 21:46, Paul wrote:
    >>>
    >>> "Pete Becker" <> wrote in message
    >>> news:2011030808444315377-pete@versatilecodingcom...
    >>>> On 2011-03-07 17:48:08 -0500, James Kanze said:
    >>>>
    >>>>> On Mar 7, 10:03 pm, Pete Becker <> wrote:
    >>>>>> On 2011-03-07 16:53:50 -0500, crea said:
    >>>>>
    >>>>> [...]
    >>>>>
    >>>>>> No, it just needs to a text string that represents the integer
    >>>>>> value in
    >>>>>> binary. Read about strtoul.
    >>>>>
    >>>>> And one that will output the integer value in binary as well.
    >>>>> That's a little harder:).
    >>>>>
    >>>>>
    >>>>
    >>>> Urk. strtoul goes the wrong way, of course.
    >>>>
    >>> #include <iostream>
    >>>
    >>> const int STR_LEN=8;
    >>> typedef char bin_str[STR_LEN];
    >>>
    >>> int main()
    >>> {
    >>> bin_str str1 = "1100110";
    >>> bin_str str2 = "1000011";
    >>> bin_str str3 = "0000000";
    >>>
    >>>
    >>> for(int x=0; x<STR_LEN; x++){
    >>> if(str1[x]=='1' && str1[x]==str2[x])
    >>> str3[x]=str1[x];
    >>> }
    >>>
    >>> std::cout<< str3;
    >>>
    >>> }
    >>>

    >>
    >> ... ...
    >> const std::size_t STR_LEN=8;
    >> ... ...
    >> for(std::size_t x=0; x != STR_LEN; x++){
    >>

    >
    > Also:
    >
    > ++x not x++
    >

    why?
    Paul, Mar 9, 2011
    #13
  14. yagru_alvarez

    Paul Guest

    "Leigh Johnston" <> wrote in message
    news:...
    > On 08/03/2011 21:46, Paul wrote:
    >>
    >> "Pete Becker" <> wrote in message
    >> news:2011030808444315377-pete@versatilecodingcom...
    >>> On 2011-03-07 17:48:08 -0500, James Kanze said:
    >>>
    >>>> On Mar 7, 10:03 pm, Pete Becker <> wrote:
    >>>>> On 2011-03-07 16:53:50 -0500, crea said:
    >>>>
    >>>> [...]
    >>>>
    >>>>> No, it just needs to a text string that represents the integer value
    >>>>> in
    >>>>> binary. Read about strtoul.
    >>>>
    >>>> And one that will output the integer value in binary as well.
    >>>> That's a little harder:).
    >>>>
    >>>>
    >>>
    >>> Urk. strtoul goes the wrong way, of course.
    >>>

    >> #include <iostream>
    >>
    >> const int STR_LEN=8;
    >> typedef char bin_str[STR_LEN];
    >>
    >> int main()
    >> {
    >> bin_str str1 = "1100110";
    >> bin_str str2 = "1000011";
    >> bin_str str3 = "0000000";
    >>
    >>
    >> for(int x=0; x<STR_LEN; x++){
    >> if(str1[x]=='1' && str1[x]==str2[x])
    >> str3[x]=str1[x];
    >> }
    >>
    >> std::cout<< str3;
    >>
    >> }
    >>

    >
    > ... ...
    > const std::size_t STR_LEN=8;


    no.
    > ... ...
    > for(std::size_t x=0; x != STR_LEN; x++){
    >

    no.
    > or use pointers (in lieu of iterators).
    >
    > Also:
    >
    > In C++ a member function is a member of class not a member of an object;

    no.
    nonstatic member functions are only invoked with an object of the class
    type.
    static member functions are part of a class not an object.

    > in C++ an object is simply a region of storage.

    no.
    There is no "simply"
    Paul, Mar 9, 2011
    #14
  15. yagru_alvarez

    Paul Guest

    "Leigh Johnston" <> wrote in message
    news:...
    > On 08/03/2011 22:23, Leigh Johnston wrote:
    >> On 08/03/2011 21:46, Paul wrote:
    >>>
    >>> "Pete Becker" <> wrote in message
    >>> news:2011030808444315377-pete@versatilecodingcom...
    >>>> On 2011-03-07 17:48:08 -0500, James Kanze said:
    >>>>
    >>>>> On Mar 7, 10:03 pm, Pete Becker <> wrote:
    >>>>>> On 2011-03-07 16:53:50 -0500, crea said:
    >>>>>
    >>>>> [...]
    >>>>>
    >>>>>> No, it just needs to a text string that represents the integer
    >>>>>> value in
    >>>>>> binary. Read about strtoul.
    >>>>>
    >>>>> And one that will output the integer value in binary as well.
    >>>>> That's a little harder:).
    >>>>>
    >>>>>
    >>>>
    >>>> Urk. strtoul goes the wrong way, of course.
    >>>>
    >>> #include <iostream>
    >>>
    >>> const int STR_LEN=8;
    >>> typedef char bin_str[STR_LEN];
    >>>
    >>> int main()
    >>> {
    >>> bin_str str1 = "1100110";
    >>> bin_str str2 = "1000011";
    >>> bin_str str3 = "0000000";
    >>>
    >>>
    >>> for(int x=0; x<STR_LEN; x++){
    >>> if(str1[x]=='1' && str1[x]==str2[x])
    >>> str3[x]=str1[x];
    >>> }
    >>>
    >>> std::cout<< str3;
    >>>
    >>> }
    >>>

    >>
    >> ... ...
    >> const std::size_t STR_LEN=8;
    >> ... ...
    >> for(std::size_t x=0; x != STR_LEN; x++){
    >>

    >
    > Also:
    >
    > ++x not x++
    >
    > (missed that due to presumption of non-n00bness)
    >

    There is no difference in this case, you are obviously the noob.
    Paul, Mar 9, 2011
    #15
  16. yagru_alvarez

    Paul Guest

    "Pete Becker" <> wrote in message
    news:2011030818103862137-pete@versatilecodingcom...
    > On 2011-03-08 16:46:37 -0500, Paul said:
    >
    >> "Pete Becker" <> wrote in message
    >> news:2011030808444315377-pete@versatilecodingcom...
    >>> On 2011-03-07 17:48:08 -0500, James Kanze said:
    >>>
    >>>> On Mar 7, 10:03 pm, Pete Becker <> wrote:
    >>>>> On 2011-03-07 16:53:50 -0500, crea said:
    >>>>
    >>>> [...]
    >>>>
    >>>>> No, it just needs to a text string that represents the integer value
    >>>>> in
    >>>>> binary. Read about strtoul.
    >>>>
    >>>> And one that will output the integer value in binary as well.
    >>>> That's a little harder:).
    >>>>
    >>>>
    >>>
    >>> Urk. strtoul goes the wrong way, of course.
    >>>

    >> #include <iostream>
    >>
    >> const int STR_LEN=8;
    >> typedef char bin_str[STR_LEN];
    >>
    >> int main()
    >> {
    >> bin_str str1 = "1100110";
    >> bin_str str2 = "1000011";
    >> bin_str str3 = "0000000";
    >>
    >>
    >> for(int x=0; x<STR_LEN; x++){
    >> if(str1[x]=='1' && str1[x]==str2[x])
    >> str3[x]=str1[x];
    >> }
    >>
    >> std::cout<< str3;
    >>
    >> }

    >
    > Shrug. The discussion was about doing this with integer values. It's
    > already been stated several times that doing it character by character is
    > better.
    >

    No it might actually be more efficient doing it with integer values ,
    example:

    const int STR_LEN=8;
    typedef unsigned char bin_str[STR_LEN];

    int main()
    {
    bin_str str1 = "1100110";
    bin_str str2 = "1000011";
    bin_str str3 = "0000000";

    for(int i=0; i<STR_LEN; i++){
    str3 = str1& str2;
    }
    }


    The best solution might be to overload the operator& in a specific string
    class.
    Paul, Mar 9, 2011
    #16
  17. yagru_alvarez

    Paul Guest

    "Pete Becker" <> wrote in message
    news:2011030818103862137-pete@versatilecodingcom...
    > On 2011-03-08 16:46:37 -0500, Paul said:
    >
    >> "Pete Becker" <> wrote in message
    >> news:2011030808444315377-pete@versatilecodingcom...
    >>> On 2011-03-07 17:48:08 -0500, James Kanze said:
    >>>
    >>>> On Mar 7, 10:03 pm, Pete Becker <> wrote:
    >>>>> On 2011-03-07 16:53:50 -0500, crea said:
    >>>>
    >>>> [...]
    >>>>
    >>>>> No, it just needs to a text string that represents the integer value
    >>>>> in
    >>>>> binary. Read about strtoul.
    >>>>
    >>>> And one that will output the integer value in binary as well.
    >>>> That's a little harder:).
    >>>>
    >>>>
    >>>
    >>> Urk. strtoul goes the wrong way, of course.
    >>>

    >> #include <iostream>
    >>
    >> const int STR_LEN=8;
    >> typedef char bin_str[STR_LEN];
    >>
    >> int main()
    >> {
    >> bin_str str1 = "1100110";
    >> bin_str str2 = "1000011";
    >> bin_str str3 = "0000000";
    >>
    >>
    >> for(int x=0; x<STR_LEN; x++){
    >> if(str1[x]=='1' && str1[x]==str2[x])
    >> str3[x]=str1[x];
    >> }
    >>
    >> std::cout<< str3;
    >>
    >> }

    >
    > Shrug. The discussion was about doing this with integer values. It's
    > already been stated several times that doing it character by character is
    > better.
    >
    > --

    My Bad, just noticed OP said LOGICAL AND, just do a string compare.
    Paul, Mar 9, 2011
    #17
  18. yagru_alvarez

    Paul Guest

    "Leigh Johnston" <> wrote in message
    news:...
    > On 09/03/2011 00:18, Paul wrote:
    >>
    >> "Leigh Johnston" <> wrote in message
    >> news:...
    >>> On 08/03/2011 22:23, Leigh Johnston wrote:
    >>>> On 08/03/2011 21:46, Paul wrote:
    >>>>>
    >>>>> "Pete Becker" <> wrote in message
    >>>>> news:2011030808444315377-pete@versatilecodingcom...
    >>>>>> On 2011-03-07 17:48:08 -0500, James Kanze said:
    >>>>>>
    >>>>>>> On Mar 7, 10:03 pm, Pete Becker <> wrote:
    >>>>>>>> On 2011-03-07 16:53:50 -0500, crea said:
    >>>>>>>
    >>>>>>> [...]
    >>>>>>>
    >>>>>>>> No, it just needs to a text string that represents the integer
    >>>>>>>> value in
    >>>>>>>> binary. Read about strtoul.
    >>>>>>>
    >>>>>>> And one that will output the integer value in binary as well.
    >>>>>>> That's a little harder:).
    >>>>>>>
    >>>>>>>
    >>>>>>
    >>>>>> Urk. strtoul goes the wrong way, of course.
    >>>>>>
    >>>>> #include <iostream>
    >>>>>
    >>>>> const int STR_LEN=8;
    >>>>> typedef char bin_str[STR_LEN];
    >>>>>
    >>>>> int main()
    >>>>> {
    >>>>> bin_str str1 = "1100110";
    >>>>> bin_str str2 = "1000011";
    >>>>> bin_str str3 = "0000000";
    >>>>>
    >>>>>
    >>>>> for(int x=0; x<STR_LEN; x++){
    >>>>> if(str1[x]=='1' && str1[x]==str2[x])
    >>>>> str3[x]=str1[x];
    >>>>> }
    >>>>>
    >>>>> std::cout<< str3;
    >>>>>
    >>>>> }
    >>>>>
    >>>>
    >>>> ... ...
    >>>> const std::size_t STR_LEN=8;
    >>>> ... ...
    >>>> for(std::size_t x=0; x != STR_LEN; x++){
    >>>>
    >>>
    >>> Also:
    >>>
    >>> ++x not x++
    >>>

    >> why?

    >
    > Because it is a bad habit not to; unless you really need post-increment
    > you should automatically use pre-increment (even if there is likely no
    > difference on your implementation as is probably the case in this case).
    >
    > The fact that you did not automatically choose pre-increment implies you
    > are lacking muscle memory in this area which implies n00bness on your
    > part.
    >

    Not at all , this just demonstates your "use the same thing all time" way of
    thinking.

    i.e:
    always use pre-inc.
    always use std::size_t
    always use std:vector insted dynamic arrays
    etc etc

    In the example I gave post/pre inc makes no difference at all, and this was
    considered as I wrote the loop.
    It's in no way a habit and I normally do use pre-inc in loops but I know it
    makes no difference so I am free to use either.
    Paul, Mar 9, 2011
    #18
  19. yagru_alvarez

    Paul Guest

    "Leigh Johnston" <> wrote in message
    news:...
    > On 09/03/2011 10:58, Paul wrote:
    >>
    >> "Leigh Johnston" <> wrote in message
    >> news:...
    >>> On 09/03/2011 00:18, Paul wrote:
    >>>>
    >>>> "Leigh Johnston" <> wrote in message
    >>>> news:...
    >>>>> On 08/03/2011 22:23, Leigh Johnston wrote:
    >>>>>> On 08/03/2011 21:46, Paul wrote:
    >>>>>>>
    >>>>>>> "Pete Becker" <> wrote in message
    >>>>>>> news:2011030808444315377-pete@versatilecodingcom...
    >>>>>>>> On 2011-03-07 17:48:08 -0500, James Kanze said:
    >>>>>>>>
    >>>>>>>>> On Mar 7, 10:03 pm, Pete Becker <> wrote:
    >>>>>>>>>> On 2011-03-07 16:53:50 -0500, crea said:
    >>>>>>>>>
    >>>>>>>>> [...]
    >>>>>>>>>
    >>>>>>>>>> No, it just needs to a text string that represents the integer
    >>>>>>>>>> value in
    >>>>>>>>>> binary. Read about strtoul.
    >>>>>>>>>
    >>>>>>>>> And one that will output the integer value in binary as well.
    >>>>>>>>> That's a little harder:).
    >>>>>>>>>
    >>>>>>>>>
    >>>>>>>>
    >>>>>>>> Urk. strtoul goes the wrong way, of course.
    >>>>>>>>
    >>>>>>> #include <iostream>
    >>>>>>>
    >>>>>>> const int STR_LEN=8;
    >>>>>>> typedef char bin_str[STR_LEN];
    >>>>>>>
    >>>>>>> int main()
    >>>>>>> {
    >>>>>>> bin_str str1 = "1100110";
    >>>>>>> bin_str str2 = "1000011";
    >>>>>>> bin_str str3 = "0000000";
    >>>>>>>
    >>>>>>>
    >>>>>>> for(int x=0; x<STR_LEN; x++){
    >>>>>>> if(str1[x]=='1' && str1[x]==str2[x])
    >>>>>>> str3[x]=str1[x];
    >>>>>>> }
    >>>>>>>
    >>>>>>> std::cout<< str3;
    >>>>>>>
    >>>>>>> }
    >>>>>>>
    >>>>>>
    >>>>>> ... ...
    >>>>>> const std::size_t STR_LEN=8;
    >>>>>> ... ...
    >>>>>> for(std::size_t x=0; x != STR_LEN; x++){
    >>>>>>
    >>>>>
    >>>>> Also:
    >>>>>
    >>>>> ++x not x++
    >>>>>
    >>>> why?
    >>>
    >>> Because it is a bad habit not to; unless you really need
    >>> post-increment you should automatically use pre-increment (even if
    >>> there is likely no difference on your implementation as is probably
    >>> the case in this case).
    >>>
    >>> The fact that you did not automatically choose pre-increment implies
    >>> you are lacking muscle memory in this area which implies n00bness on
    >>> your part.
    >>>

    >> Not at all , this just demonstates your "use the same thing all time"
    >> way of thinking.
    >>
    >> i.e:
    >> always use pre-inc.
    >> always use std::size_t
    >> always use std:vector insted dynamic arrays
    >> etc etc
    >>
    >> In the example I gave post/pre inc makes no difference at all, and this
    >> was considered as I wrote the loop.

    >
    > You are a liar.


    Why would I need to lie?

    >
    >> It's in no way a habit and I normally do use pre-inc in loops but I know
    >> it makes no difference so I am free to use either.

    >
    > FAIL. PWNED.
    >

    eh ?
    Failed at what? you're the one who failed.
    You tried to make a correction but your correction was no different from the
    code you were trying to correct.
    Get lost you idiot.
    Paul, Mar 9, 2011
    #19
  20. yagru_alvarez

    Paul Guest

    "Pete Becker" <> wrote in message
    news:2011030910065089745-pete@versatilecodingcom...
    > On 2011-03-08 20:34:00 -0500, Paul said:
    >
    >> "Pete Becker" <> wrote in message
    >> news:2011030818103862137-pete@versatilecodingcom...
    >>> On 2011-03-08 16:46:37 -0500, Paul said:
    >>>
    >>>> "Pete Becker" <> wrote in message
    >>>> news:2011030808444315377-pete@versatilecodingcom...
    >>>>> On 2011-03-07 17:48:08 -0500, James Kanze said:
    >>>>>
    >>>>>> On Mar 7, 10:03 pm, Pete Becker <> wrote:
    >>>>>>> On 2011-03-07 16:53:50 -0500, crea said:
    >>>>>>
    >>>>>> [...]
    >>>>>>
    >>>>>>> No, it just needs to a text string that represents the integer value
    >>>>>>> in
    >>>>>>> binary. Read about strtoul.
    >>>>>>
    >>>>>> And one that will output the integer value in binary as well.
    >>>>>> That's a little harder:).
    >>>>>>
    >>>>>>
    >>>>>
    >>>>> Urk. strtoul goes the wrong way, of course.
    >>>>>
    >>>> #include <iostream>
    >>>>
    >>>> const int STR_LEN=8;
    >>>> typedef char bin_str[STR_LEN];
    >>>>
    >>>> int main()
    >>>> {
    >>>> bin_str str1 = "1100110";
    >>>> bin_str str2 = "1000011";
    >>>> bin_str str3 = "0000000";
    >>>>
    >>>>
    >>>> for(int x=0; x<STR_LEN; x++){
    >>>> if(str1[x]=='1' && str1[x]==str2[x])
    >>>> str3[x]=str1[x];
    >>>> }
    >>>>
    >>>> std::cout<< str3;
    >>>>
    >>>> }
    >>>
    >>> Shrug. The discussion was about doing this with integer values. It's
    >>> already been stated several times that doing it character by character
    >>> is better.
    >>>

    >> No it might actually be more efficient doing it with integer values ,
    >> example:

    >
    > Since you've snipped all relevant context, there is no meaningful way to
    > reply to your irrelevant example.
    >
    >>
    >> const int STR_LEN=8;
    >> typedef unsigned char bin_str[STR_LEN];
    >>
    >> int main()
    >> {
    >> bin_str str1 = "1100110";
    >> bin_str str2 = "1000011";
    >> bin_str str3 = "0000000";
    >>
    >> for(int i=0; i<STR_LEN; i++){
    >> str3 = str1& str2;
    >> }
    >> }
    >>
    >>
    >> The best solution might be to overload the operator& in a specific string
    >> class.

    >
    > Sure. But that's not what this subthread was about, so please stop
    > pretending that you've had some insight that's actually useful.
    >

    I didn't snip anything. :)
    Paul, Mar 9, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    365
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    724
    Malcolm
    Jun 24, 2006
  3. Raj

    Some logic error in my C code

    Raj, Aug 25, 2007, in forum: C Programming
    Replies:
    4
    Views:
    266
    Keith Thompson
    Aug 25, 2007
  4. John Salerno

    Relationship between GUI and logic?

    John Salerno, May 23, 2008, in forum: Python
    Replies:
    11
    Views:
    457
    Matthew Woodcraft
    May 24, 2008
  5. spike
    Replies:
    8
    Views:
    1,427
    Steve Holden
    Feb 9, 2010
Loading...

Share This Page