473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

string operation??

hi everybody,

char str[10]="string";
str[0]^=str[1];

can anybody plz tell me the use of ^operator in string???

-Sethu

Nov 27 '06 #1
11 1874

se*****@gmail.com wrote:
hi everybody,

char str[10]="string";
str[0]^=str[1];

can anybody plz tell me the use of ^operator in string???
This isn't a mobile phone. "please" is only a little more typing than
"plz", and the shift key is easy to find. Basic typography a) improves
the readibility of your message and b) makes the reader feel that you
bothered to put in some effort.

Now to the question. In "str[0]^=str[1]" you are not operating on a
string, but on a character in that string. We are doing an exclusive
bitwise or operation with the first two characters and storing the
result in the first. Why the code is doing that is beyond me, though
I'd suspect it was a crude cryptographic technique....

Nov 27 '06 #2
se*****@gmail.com wrote:
hi everybody,

char str[10]="string";
str[0]^=str[1];

can anybody plz tell me the use of ^operator in string???
Please avoid abbreviations like plz instead of please, since this is an
international forum and many posters may not know it's meaning.

The bit pattern of the char value at str[1] is taken and an exclusive
OR operation is done with the bit pattern of the char value at str[0]
and the result in stored back to str[0].

The ^= is a compound assignment operator.

Nov 27 '06 #3
in 707477 20061127 112944 "santosh" <sa*********@gmail.comwrote:
>se*****@gmail.com wrote:
>hi everybody,

char str[10]="string";
str[0]^=str[1];

can anybody plz tell me the use of ^operator in string???

Please avoid abbreviations like plz instead of please, since this is an
international forum and many posters may not know it's meaning.

The bit pattern of the char value at str[1] is taken and an exclusive
OR operation is done with the bit pattern of the char value at str[0]
and the result in stored back to str[0].

The ^= is a compound assignment operator.
Which would be clearer with spaces either side of the operator
(especially to a Pascal programmer).
Nov 27 '06 #4
Bob Martin wrote:
in 707477 20061127 112944 "santosh" <sa*********@gmail.comwrote:
se*****@gmail.com wrote:
hi everybody,

char str[10]="string";
str[0]^=str[1];

can anybody plz tell me the use of ^operator in string???
Please avoid abbreviations like plz instead of please, since this is an
international forum and many posters may not know it's meaning.

The bit pattern of the char value at str[1] is taken and an exclusive
OR operation is done with the bit pattern of the char value at str[0]
and the result in stored back to str[0].

The ^= is a compound assignment operator.

Which would be clearer with spaces either side of the operator
(especially to a Pascal programmer).
Yes, it seems popular among many newbies to eschew whitespace, and C
seems particularly prone to this trend!

Nov 27 '06 #5
ma**********@pobox.com wrote:
char str[10]="string";
str[0]^=str[1];
Now to the question. In "str[0]^=str[1]" you are not operating on a
string, but on a character in that string. We are doing an exclusive
bitwise or operation with the first two characters and storing the
result in the first. Why the code is doing that is beyond me, though
I'd suspect it was a crude cryptographic technique....
Is it implementation-defined whether the above code yields undefined
behavior or not? (If char is signed, may str[0]^=str[1] not generate
a trap representation?)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Nov 27 '06 #6
2006-11-27 <ek**********@chessie.cirr.com>,
Christopher Benson-Manica wrote:
ma**********@pobox.com wrote:
char str[10]="string";
str[0]^=str[1];
>Now to the question. In "str[0]^=str[1]" you are not operating on a
string, but on a character in that string. We are doing an exclusive
bitwise or operation with the first two characters and storing the
result in the first. Why the code is doing that is beyond me, though
I'd suspect it was a crude cryptographic technique....

Is it implementation-defined whether the above code yields undefined
behavior or not? (If char is signed, may str[0]^=str[1] not generate
a trap representation?)
Is CHAR_MAX allowed to be a value other than a power of two minus one?
Nov 27 '06 #7
Christopher Benson-Manica wrote:
ma**********@pobox.com wrote:
char str[10]="string";
str[0]^=str[1];
Now to the question. In "str[0]^=str[1]" you are not operating on a
string, but on a character in that string. We are doing an exclusive
bitwise or operation with the first two characters and storing the
result in the first. Why the code is doing that is beyond me, though
I'd suspect it was a crude cryptographic technique....

Is it implementation-defined whether the above code yields undefined
behavior or not? (If char is signed, may str[0]^=str[1] not generate
a trap representation?)
Probably not. Footnote 44 on page 38 of N1124 says

Some combinations of padding bits might
generate trap representations, for example,
if one padding bit is a parity bit. Regardless,
no arithmetic operation on valid values can
generate a trap representation other than as
part of an exceptional condition such as an
overflow, and this cannot occur with unsigned
types.

I'm not completely certain that bitwise operators count
as arithmetic operators but they should be. Plus it
feels that char1 ^= char2 should be harmless regardless
whether char1 and char2 are signed or unsigned.

Nov 27 '06 #8
Random832 wrote:
Is CHAR_MAX allowed to be a value other than a power of two minus one?
In N1124, 6.2.6.2., paragraph 1 we read

For unsigned integer types other than
unsigned char, the bits of the object
representation shall be divided into two
groups: value bits and padding bits (there
need not be any of the latter). If there are N
value bits, each bit shall represent a different
power of 2 between 1 and 2^(N-1), so that objects
of that type shall be capable of representing values
from 0 to 2^N - 1 using a pure binary representation;
this shall be known as the value representation.

And in paragraph 2 we read

For signed integer types, the bits of the object
representation shall be divided into three groups:
value bits, padding bits, and the sign bit. There
need not be any padding bits; there shall be exactly
one sign bit. Each bit that is a value bit shall have
the same value as the same bit in the object
representation of the corresponding unsigned type
(if there are M value bits in the signed type and N in
the unsigned type, then M <= N).
>From the above I'm led to think that the maximum value
of any integer type is a power of 2 minus 1.

Nov 27 '06 #9
Spiros Bousbouras wrote:
Christopher Benson-Manica wrote:
ma**********@pobox.com wrote:
char str[10]="string";
str[0]^=str[1];
Now to the question. In "str[0]^=str[1]" you are not operating on a
string, but on a character in that string. We are doing an exclusive
bitwise or operation with the first two characters and storing the
result in the first. Why the code is doing that is beyond me, though
I'd suspect it was a crude cryptographic technique....
Is it implementation-defined whether the above code yields undefined
behavior or not? (If char is signed, may str[0]^=str[1] not generate
a trap representation?)

Probably not. Footnote 44 on page 38 of N1124 says

Some combinations of padding bits might
generate trap representations, for example,
if one padding bit is a parity bit. Regardless,
no arithmetic operation on valid values can
generate a trap representation other than as
part of an exceptional condition such as an
overflow, and this cannot occur with unsigned
types.

I'm not completely certain that bitwise operators count
as arithmetic operators but they should be.
They are, however this footnote only addresses trap representations as
a result of padding bits, which makes sense since it is attached to a
description of unsigned types. Signed types have another possibility
for a trap representation.
Plus it
feels that char1 ^= char2 should be harmless regardless
whether char1 and char2 are signed or unsigned.
If CHAR_MIN equals -CHAR_MAX and the implementation does not support
negative zeroes, it's possible to generate a trap representation using
the bitwise operators. However, since for this the sign bit must be
set, and 's' and 't' must both be positive, there's no problem with the
original code.

Nov 27 '06 #10
2006-11-27 <11*********************@l12g2000cwl.googlegroups. com>,
Spiros Bousbouras wrote:
Random832 wrote:
>Is CHAR_MAX allowed to be a value other than a power of two minus one?
[snip]
>
From the above I'm led to think that the maximum value
of any integer type is a power of 2 minus 1.
In that case, regarding the question which you snipped about 's'^'t'
yielding a trap representation, it's not possible, since the xor
operation is defined mathematically in such a way as that if you take
any two positive numbers less than or equal CHAR_MAX, the result will be
another positive number less than or equal to CHAR_MAX. And characters
in the basic execution character set are guaranteed to be positive.
Nov 28 '06 #11
santosh wrote:
se*****@gmail.com wrote:
>hi everybody,

char str[10]="string";
str[0]^=str[1];

can anybody plz tell me the use of ^operator in string???

The bit pattern of the char value at str[1] is taken and an exclusive
OR operation is done with the bit pattern of the char value at str[0]
and the result in stored back to str[0].
Yes. In the specific case of ASCII encoded strings, which is quite
likely, the values involved will be 115 ('s') and 116 ('t').

In decimal, 115 ^ 116 = 7

In binary, 01110011 ^ 01110100 = 00000111

Again assuming ASCII, the value 7 is the alert character '\a', also
known as BEL or CTRL-G. Attempting to print str after this xor operation
will probably result in an audible beep followed by the characters
"tring" which were unchanged.

--
Simon.
Nov 28 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Neil Schemenauer | last post by:
The title is perhaps a little too grandiose but it's the best I could think of. The change is really not large. Personally, I would be happy enough if only %s was changed and the built-in was...
11
by: ALI-R | last post by:
why c# function for string manupolation is not as strong as other languages ,for extracting a substring from a middle of another string ,I have a lot of problems. Any suggestion or I am in the...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
1
by: varunhome | last post by:
Hi, I want to check for the absence of a string in regular expression. For example, if the string is "Error opening file: Permission denied. Aborting.", I want to check for absence of the string...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
13
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I...
6
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my...
0
by: Bart Kastermans | last post by:
|    def __str__ (self): I did some timing of operations involved. Doing this I found that the operation below to get a string representation for trees was in fact not quadratic. The final...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.