Re: Bit Pattern Problem

From: William L. Bahn (william_at_toomuchspam.net)
Date: 09/02/04


Date: Thu, 2 Sep 2004 02:24:05 -0600


"fb" <fb@goaway.net> wrote in message
news:LAuZc.299540$gE.33632@pd7tw3no...
> Hi Everyone. Thanks for the help with the qudratic equation
problem...I
> didn't think about actually doing the math...whoops.

It's actually a very, very big oops that I hope you learn from
and don't just hand wave it.

Computers are really, really stupid. They crunch numbers - and
that's about it. It is up to YOU to solve the problem. Before you
start coding you should try some hand cases and crank the math
and you should especially do that as soon as a problem arises.
There seems to be an ever increasing trend towards just wanting
to throw code at the compiler without understanding what that
code does or is even supposed to do. Don't let yourself fall into
that trap.

>Anyway... I'm
> having some trouble getting the following program to work. I
want to
> output a bit pattern from base 10 input. All I get is a zero
after the
> input...I've looked over the code but can't see the
problem...any ideas?
>

All you get is "a zero"? As in a single zero? Even though you
have a loop that is guaranteed to print out something every time
through it? What does that tell you? Either the test only passes
the first time through, or the loop code isn't the code you think
it is. If examining the code with this in mind doesn't tell you
something, then instrument the loop by printing out the value of
count within the body of the loop. If it prints out "1" then the
loop is only executing once - look for something in the loop that
is changing one of the values in the test expression. If it
prints out a "9" then what you thought was the loop is not
actually part of the loop. There's a REAL common mistake that can
result in that - so learning how to track it down through
systematic debugging when the Mark II eyeballs insist on skipping
over it (which that are want to do because, as a human with a
functioning human brain, you automatically tend to see what you
WANT the code to be instead of what the code actually IS) can be
a very valuable lesson. I'll leave it for you to track down this
problem further.

You have another potential problem in that any time you do right
shift operations on signed integers, you don't know for sure what
it going to get shifted in to the vacated bit position. You also
don't know for sure how negative values are represented - but
that's sort of the point of the exercise, isn't it?

This is my take on performing any kind of bit banging - the folks
that wrote C intended it to be primarily a high level language
where the programmer worked with "values" and not
"representations". They therefore primarily define how the
different operations behave with respect to the values being
represented and not the representations themselves. They leave it
up to the compiler designer to make that happen and leave them
free to play whatever games they like to (subject to a few
constraints) with the internal representations. But, they
recognize that a few people will have the need/desire to do bit
banging and so they defined a minimal set of operations that have
to have completely well-defined effects on the internal
representation - but imposing this constraint imposes unnecessary
limitations on the games the compiler writer can play in working
with the "values", so the ONLY data type (as far as I know) that
they included in this tightly constrained bit-level definition
are the unsigned integers. Not the integers, only the unsigned
integers. Therefore, only bit bang unsigned integers.

>
> /* display the bit pattern corresponding to
> a signed decimal integer */
>
> #include<stdio.h>
> #include<stdlib.h>
>
> int main(void)
> {
> int a, b, m, count, nbits;
> unsigned mask;
>
> /* determine the word size in bits and set the initial mask
*/
> nbits = 8 * sizeof(int);
> m = 0x1 << (nbits - 1); /* Place 1 in leftmost position */
>
> /* main loop */
> do {
> /* read a signed integer */
> printf("\n\nEnter an integer value (0 to stop): ", a);
> scanf("%d", &a);
>
> /* output the bit pattern */
> mask = m;
> for(count = 1; count <= nbits; count++); {
> b = (a & mask) ? 1 : 0; /* set display bit on or off */
> printf("%x", b); /* print display bit */
> if (count % 4 == 0)
> printf(" "); /* blank space after ever 4th digit */
> mask >>= 1; /* shift mask 1 position to the right */
> }
> } while (a != 0);
> return EXIT_SUCCESS; /* If it's there why not use it
people! */
> }
>



Relevant Pages

  • Re: isolation level serializable
    ... end loop; ... on to disable all constraints on the destination tables, ... set transaction isolation level serializable; ...
    (comp.databases.oracle.server)
  • Re: Heathrow T4 tube station open again
    ... Anyway, I don't think that there's room west of Hatton Cross for a crossover to the eastbound track. ... So now you're talking major reconstruction as well as resignalling the loop. ... The former has the problem that the single line between T3 and T4 puts heavy constraints on how frequent the trains can be. ...
    (uk.transport.london)
  • Re: Keep Task Days Together
    ... To avoid that I put a loop that removes all previously placed ... > constraints before re-applying them. ... macro - that may not always be the case (i.e. some tasks in a project ... John ...
    (microsoft.public.project)
  • Re: How to speed up looping through controls (used for restore and maximize)?
    ... line before the loop (the reason being that it looked weird to change ... changing the constraints, and 'LockWindowupdate;' after the loop. ... So now the controls move, ...
    (comp.lang.pascal.delphi.misc)