Re: possible overflow?



Rob wrote:
) Peter Nilsson wrote:
)
)> while (mask != 0)
)> {
)> putchar('0' + !!(input & mask));
)> mask >>= 1;
)> }
)
) Ok, the double negation operation is a little hazy in my mind. I was going
) to try and explain what I thought it is doing, but that seems to not be
) working. I was able to turn up something regarding the double '!!' which
) Dan Pop wrote on comp.lang.c, but his explanation was brief. Would someone
) care to explain what is happening with
)
) putchar('0' + !!(input & mask));
)
) and how it works?

!0 = 1
!1 = 0
!2 = 0
etc.

Another negation swaps the 0 and 1 again, so basically 0 remains 0,
and any other value becomes 1.

'mask' has a single bit set, each loop it's shifted one position.

If you AND 'input' with 'mask', the result is zero when the Nth bit
of 'input' is clear, and it's non-zero when it's set.

So, !!(input & mask) equals 0 if the 'current' bit is clear,
and 1 if it is set.

'0' is the ASCII value of the character 0.
If you add one to that, you get the ASCII value of the character 1.

So, the end result is '0' if the bit is clear, and '1' if it is set.


A direct and clear way to write this would be:

putchar((input & mask) ? '1' : '0');


But that might(*) be less efficient, so the double-negation trick
is favoured by some programmers.
However, the following might even be more efficient:

putchar('1' - !(input & mask));

Try to work out for yourself how that one works.


*) With any luck, a decent optimizing compiler will produce
equally fast code for the clear way, and it might even be faster.
For the record: gcc produces *the same* code for all three lines.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
.



Relevant Pages

  • Re: possible overflow?
    ... > 'mask' has a single bit set, each loop it's shifted one position. ... > If you add one to that, you get the ASCII value of the character 1. ...
    (comp.programming)
  • Re: what does "serialization" mean?
    ... Sorry eddie, but you're dead wrong there as usual. ... >>How about ASCII character 0xB0, ... > Totalitarians and Fascists are often self-appointed language police. ...
    (comp.programming)
  • Re: what does "serialization" mean?
    ... > attempt to present myself as an authority on any and every topic I have ... >> survived and EBCDIC did not because ASCII properly sequenced letters. ... > How about ASCII character 0xB0, ... >> must assert negative facts, for all he knows is there is no knowledge ...
    (comp.programming)
  • Re: Cohens paper on byte order
    ... I think you're using "ASCII" in a notional sense. ... a good reason to teach the *opposite* convention, ... Computers should be as easy to understand as is possible _without_ ... arithmetic on character strings ...
    (sci.crypt)
  • Re: Reading a file.
    ... your program will interpret them as ASCII. ... Bruce.Eitman AT EuroTech DOT com ... buffer is character values, then in memory ASCII values are displayed. ... DWORD d = GetLastError; ...
    (microsoft.public.windowsce.app.development)

Loading