Re: string operation??



Christopher Benson-Manica wrote:
mark_bluemel@xxxxxxxxx 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.

.



Relevant Pages

  • Re: string operation??
    ... string, but on a character in that string. ... I'm not completely certain that bitwise operators count ... for a trap representation. ...
    (comp.lang.c)
  • Re: string operation??
    ... string, but on a character in that string. ... bitwise or operation with the first two characters and storing the ...
    (comp.lang.c)
  • Re: string operation??
    ... string, but on a character in that string. ... bitwise or operation with the first two characters and storing the ...
    (comp.lang.c)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)