Complicated expression.Pls help

Discussion in 'C Programming' started by sathyashrayan, Apr 9, 2005.

  1. Groups,
    Take a look at the following program taken from C snippet archive.

    -----------------------code------------------------

    void bitstring(char *str, long byze, int biz, int strwid)
    {
    int i, j;
    j = strwid - (biz + (biz >> 2)- (biz % 4 ? 0 : 1));
    for (i = 0; i < j; i++)
    *str++ = ' ';
    while (--biz >= 0)
    {
    *str++ = ((byze >> biz) & 1) + '0';
    if (!(biz % 4) && biz)
    *str++ = ' ';
    }
    *str = '\0';
    }

    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    char s[80]; long j;
    for (j = 1L; j <= 16L; j++)
    {
    bitstring(s, (long)j, (int)j, 16);
    printf("%2ld: %s\n", j, s);
    }
    return EXIT_SUCCESS;
    }

    ------------------------output--------------------

    1: 1
    2: 10
    3: 011
    4: 0100
    5: 0 0101
    6: 00 0110
    7: 000 0111
    8: 0000 1000
    9: 0 0000 1001
    10: 00 0000 1010
    11: 000 0000 1011
    12: 0000 0000 1100
    13: 0 0000 0000 1101
    14: 00 0000 0000 1110
    15: 000 0000 0000 1111
    16: 0000 0000 0001 0000

    --------------------------------------end--------------------

    My doubts are:
    1) In the expression
    j = strwid - (biz + (biz >> 2)- (biz % 4 ? 0 : 1));
    the variable biz is right shifted. When I looked at the called
    function (which happens to be main()) the loop(var j) iterates from 1 to 16.
    Then that must defiantly crosses the value 3. According to the std 99, 6.5.7.3
    "....If the value of the right operand is negative or is
    greater than or equal to the width of the promoted left operand, the
    behavior is undefined."
    Does the above invokes UB? How shall I interpret the word *width* in the
    statement. Does statement considered the value only or the sizeof the left operand.

    2)Why the number 4 is been used in the MOD operator?Is it a magic number?

    3) Does the variable biz is changed more than once between the sequence point?

    <OT to comp.lang.c>
    Can anybody explain the function bitstring(). I took a paper and pen, I am not able to proceed
    further.
    </OT to comp.lang.c>
    sathyashrayan, Apr 9, 2005
    #1
    1. Advertising

  2. In article <42579be9$0$882$>,
    sathyashrayan <> wrote:
    >My doubts are:
    > 1) In the expression
    > j = strwid - (biz + (biz >> 2)- (biz % 4 ? 0 : 1));
    > the variable biz is right shifted. When I looked at the called
    >function (which happens to be main()) the loop(var j) iterates from 1 to 16.
    >Then that must defiantly crosses the value 3. According to the std 99, 6.5.7.3
    > "....If the value of the right operand is negative or is
    > greater than or equal to the width of the promoted left operand, the
    > behavior is undefined."
    > Does the above invokes UB?


    No, not for that line. The later line that shifts by biz is also
    safe because of the decrement of biz's value -before- the first
    shift, combined with the fact that int must be at least 16 bits
    in C... thus you do not cannot hit the boundary case of the right operand
    being -equal to- the width of the left operand.


    >How shall I interpret the word *width* in the
    > statement. Does statement considered the value only or the sizeof the left operand.


    sizeof times CHAR_BITS -- that is, it is the width in bits that
    is relevant for >> .


    > 2)Why the number 4 is been used in the MOD operator?Is it a magic number?


    Well, in a sense. The output format is "magic"... and happens to
    be in groups of four bits. The 4 of the mod operator is -directly-
    related to that grouping by four bits.


    > 3) Does the variable biz is changed more than once between the sequence point?


    No, the *only* place that the variable biz is modified is the
    decrement at the start of the while loop.


    > Can anybody explain the function bitstring(). I took a paper and pen, I am not able to proceed
    > further.


    It's pretty straight forward. j is a calculation of the number
    of character positions which will be required in the string, and then
    rest of it is just filling in those character positions with
    a mix of 0's for 0 bits, 1's for 1 bits, and spaces to separate
    the groups of 4.

    I suspect you did not happen to notice the equivilence between
    (shifting biz right by 2 bits) and (dividing biz by 4).

    --
    Are we *there* yet??
    Walter Roberson, Apr 9, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JFizzR
    Replies:
    1
    Views:
    311
    Andrew Thompson
    Nov 30, 2003
  2. Mel
    Replies:
    2
    Views:
    338
    Richard
    Dec 13, 2004
  3. Mark Mackey

    Complicated circular type definitions: help!

    Mark Mackey, Nov 19, 2003, in forum: C Programming
    Replies:
    2
    Views:
    374
    Mark Mackey
    Nov 19, 2003
  4. Freshman_in_C_Programming

    Basic but complicated problem....Help me Plz!!!

    Freshman_in_C_Programming, Feb 1, 2006, in forum: C Programming
    Replies:
    24
    Views:
    737
    Ed Prochak
    Feb 6, 2006
  5. Allawy
    Replies:
    0
    Views:
    715
    Allawy
    Aug 18, 2008
Loading...

Share This Page