Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Logic AND between some strings

Reply
Thread Tools

Logic AND between some strings

 
 
yagru_alvarez
Guest
Posts: n/a
 
      03-07-2011
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-07-2011
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
 
Reply With Quote
 
 
 
 
yagru_alvarez
Guest
Posts: n/a
 
      03-07-2011
On Mar 7, 9:23*am, Victor Bazarov <(E-Mail Removed)> 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
 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      03-07-2011
"yagru_alvarez" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> 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.
 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      03-07-2011
"Fred Zwarts" <(E-Mail Removed)> wrote in message
news:il31an$gli$(E-Mail Removed)
> "yagru_alvarez" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)
>> 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.

 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      03-07-2011
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;
}

.


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-07-2011
On 3/7/2011 10:31 AM, yagru_alvarez wrote:
> On Mar 7, 9:23 am, Victor Bazarov<(E-Mail Removed)> 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
 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      03-07-2011
Paavo Helde wrote:
> "crea" <(E-Mail Removed)> wrote in
> news:qO7dp.24381$(E-Mail Removed)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.



 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      03-07-2011
Paavo Helde wrote:
> "crea" <(E-Mail Removed)> wrote in
> news:Iz9dp.103060$(E-Mail Removed)2:
>
>> Paavo Helde wrote:
>>> "crea" <(E-Mail Removed)> wrote in
>>> news:qO7dp.24381$(E-Mail Removed)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


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-07-2011
On Mar 7, 10:03 pm, Pete Becker <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Logic Map/Logic Flow Chart. (Example Provided) spike Python 8 02-09-2010 12:31 PM
Asynchronous Logic Gates and Analog Logic Gates Jyoti Ballabh Software 3 11-26-2009 06:48 PM
Relationship between GUI and logic? John Salerno Python 11 05-24-2008 06:22 PM
Want overview for customizing some of the forms authentication logic brianjadams@gmail.com ASP .Net 0 08-02-2006 05:45 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM



Advertisments