473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Complicated expression.Pls help

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>
Nov 14 '05 #1
1 1277
In article <42*********************@authen.white.readfreenews .net>,
sathyashrayan <sa***********@REMOVETHISgmail.com> 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??
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
4
by: d.p. | last post by:
I need to create a conditional expression that's not a simple one. I need the expression to be in the field of a table, that depends on another field (different column) in that same table. It...
2
by: Mike Turco | last post by:
I like using the expression builder for a lot of different things but it isn't always available when I want to use it, for example in the code window, or in all of the control properties. I am...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
1
by: paul_zaoldyeck | last post by:
Good Day everyone!!! i have here a serious problem.my project is delayed because of this. i'm trying to make a function for an XML Reader that accepts values from the other modules(done by the...
5
by: jayanthigk2004 | last post by:
Is it possible to write a regular expression for this ? Pattern: 999-99-999 Where 9 is any number from 0 to 9 However the user need not enter ALL the digits and dashes as given in the...
4
by: carlos | last post by:
I am working on a regular expression validation for my search page. What I have so far works for most cases, but I would like to fine tune it some. I am new to regular expressions, and I do not...
7
by: MarkNeumann | last post by:
I'm coming from a Corel paradox background and moving into an Access environment. So I'm struggling with something that I think is probably way simpler than I'm making it out to be. Access 2007...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.