Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Complicated expression.Pls help

Reply
Thread Tools

Complicated expression.Pls help

 
 
sathyashrayan
Guest
Posts: n/a
 
      04-09-2005
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>


 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      04-09-2005
In article <42579be9$0$882$(E-Mail Removed) .net>,
sathyashrayan <(E-Mail Removed)> 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??
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic but complicated problem....Help me Plz!!! Freshman_in_C_Programming C Programming 24 02-06-2006 07:08 PM
Re: need help with a complicated stored procedure please iixv Microsoft Certification 0 07-30-2005 04:34 PM
can someone help with this complicated <IFRAME>... Mel HTML 2 12-13-2004 08:24 PM
Help with program... This one is complicated... WIZ needed JFizzR Java 1 11-30-2003 01:20 AM
Complicated circular type definitions: help! Mark Mackey C Programming 2 11-19-2003 04:24 PM



Advertisments