Re: string operation??
- From: "Spiros Bousbouras" <spibou@xxxxxxxxx>
- Date: 27 Nov 2006 13:23:19 -0800
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.
.
- Follow-Ups:
- Re: string operation??
- From: Harald van Dijk
- Re: string operation??
- References:
- string operation??
- From: sethukr@xxxxxxxxx
- Re: string operation??
- From: mark_bluemel
- Re: string operation??
- From: Christopher Benson-Manica
- string operation??
- Prev by Date: Re: Reading a string of unknown size
- Next by Date: Re: string operation??
- Previous by thread: Re: string operation??
- Next by thread: Re: string operation??
- Index(es):
Relevant Pages
|
|