By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

K&R bitwise operator section

P: n/a
I am reading K&R and right now in section 2.9 bitwise operators. I
understood most of the simple things initially but getting stuck with
the later part where different functions like getbits(), setbits are
explained.

Is there a better guide to understand such bit operator problems with
some examples.

Thanks

Feb 25 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
DanielJohnson wrote:
I am reading K&R and right now in section 2.9 bitwise operators. I
understood most of the simple things initially but getting stuck with
the later part where different functions like getbits(), setbits are
explained.

Is there a better guide to understand such bit operator problems with
some examples.

Thanks
A quick Google search turns up these links:
<http://www.gamedev.net/reference/articles/article1563.asp?the_id=69>
<http://www.cs.cf.ac.uk/Dave/C/node13.html>
<http://www.eskimo.com/~scs/cclass/int/sx4ab.html>
<http://www.phim.unibe.ch/comp_doc/c_manual/C/CONCEPT/bitwise.html>

What specifically do you not understand?

Feb 25 '07 #2

P: n/a
What specifically do you not understand?

I was trying to solve the exercises 2.6-2.8 and could not get the
results. Then I googled searched for solutions and found couple of
programs for it but I could not get the logic and wanted more English
explanation for what is being done there.
Feb 25 '07 #3

P: n/a
DanielJohnson wrote:
What specifically do you not understand?

I was trying to solve the exercises 2.6-2.8 and could not get the
results. Then I googled searched for solutions and found couple of
programs for it but I could not get the logic and wanted more English
explanation for what is being done there.
Well, firstly get a copy of The C Answer Book (2nd Ed.) by Clovis L.
Tondo and Scott E. Gimpel. It has solutions to all the Exercises in
K&R2. Also the clc wiki (http://clc-wiki.net/) has some solutions as
well.

Feb 25 '07 #4

P: n/a

DanielJohnson wrote:
What specifically do you not understand?

I was trying to solve the exercises 2.6-2.8 and could not get the
results. Then I googled searched for solutions and found couple of
programs for it but I could not get the logic and wanted more English
explanation for what is being done there.
These are the solutions from the clc wiki. Don't consult them before
giving your best try at the problems.

<http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_2:Exercise_6>
<http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_2:Exercise_7>
<http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_2:Exercise_8>

Feb 25 '07 #5

P: n/a
On Feb 25, 12:47 pm, "santosh" <santosh....@gmail.comwrote:
DanielJohnson wrote:
What specifically do you not understand?
One quick question with bit operators. How can we print a number in
binary. For example 10 should print as 00001010.

Since it is possible to write octal and hex notations I was wondering
if there is anything that supports printing of binary.

Daniel

Feb 25 '07 #6

P: n/a
In article <11**********************@j27g2000cwj.googlegroups .com>,
DanielJohnson <di********@gmail.comwrote:
>One quick question with bit operators. How can we print a number in
binary. For example 10 should print as 00001010.
There's no printf() functionality to do it, but you can roll your own:

http://c-faq.com/misc/hexio.html

-Beej

Feb 25 '07 #7

P: n/a
santosh wrote:
DanielJohnson wrote:
What specifically do you not understand?

I was trying to solve the exercises 2.6-2.8 and could not get the
results. Then I googled searched for solutions and found couple of
programs for it but I could not get the logic and wanted more English
explanation for what is being done there.

Well, firstly get a copy of The C Answer Book (2nd Ed.) by Clovis L.
Tondo and Scott E. Gimpel. It has solutions to all the Exercises in
K&R2. Also the clc wiki (http://clc-wiki.net/) has some solutions as
well.
Not before you have given your best shot :-). Otherwise there is no point
solving the problems just by looking at solutions.

Tejas Kokje
Feb 26 '07 #8

P: n/a
DanielJohnson wrote:
>
.... snip ...
>
One quick question with bit operators. How can we print a number
in binary. For example 10 should print as 00001010.

Since it is possible to write octal and hex notations I was
wondering if there is anything that supports printing of binary.
Try the following:

/* Routines to display values in various bases */
/* with some useful helper routines. */
/* by C.B. Falconer, 19 Sept. 2001 */
/* Released to public domain. Attribution appreciated */

#include <stdio.h>
#include <string.h>
#include <limits.h /* ULONG_MAX etc. */

/* ======================= */
/* reverse string in place */
size_t revstring(char *stg)
{
char *last, temp;
size_t lgh;

lgh = strlen(stg);
if (lgh 1) {
last = stg + lgh; /* points to '\0' */
while (last-- stg) {
temp = *stg; *stg++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

/* ============================================ */
/* Mask and convert digit to hex representation */
/* Output range is 0..9 and a..f only */
int hexify(unsigned int value)
{
static char hexchars[] = "0123456789abcdef";

return (hexchars[value & 0xf]);
} /* hexify */

/* ================================================== */
/* convert unsigned number to string in various bases */
/* 2 <= base <= 16, controlled by hexify() */
/* Returns actual output string length */
size_t basedisplay(unsigned long number, unsigned int base,
char *stg, size_t maxlgh)
{
char *s;

/* assert (stg[maxlgh]) is valid storage */
s = stg;
if (maxlgh && base)
do {
*s = hexify(number % base);
s++;
} while (--maxlgh && (number = number / base) );
*s = '\0';
revstring(stg);
return (s - stg);
} /* basedisplay */

/* ================================================ */
/* convert signed number to string in various bases */
/* 2 <= base <= 16, controlled by hexify() */
/* Returns actual output string length */
size_t signbasedisplay(long number, unsigned int base,
char * stg, size_t maxlgh)
{
char *s;
size_t lgh;
unsigned long n;

s = stg; lgh = 0;
n = (unsigned long)number;
if (maxlgh && (number < 0L)) {
*s++ = '-';
maxlgh--;
n = -(unsigned long)number;
lgh = 1;
}
lgh = lgh + basedisplay(n, base, s, maxlgh);
return lgh;
} /* signbaseddisplay */
/* ==================== */
/* flush to end-of-line */
int flushln(FILE *f)
{
int ch;

while ('\n' != (ch = fgetc(f)) && (EOF != ch)) /* more */;
return ch;
} /* flushln */

/* ========== END of generically useful routines ============ */

/* ========================= */
/* Prompt and await <return*/
static void nexttest(char *prompt)
{
static char empty[] = "";

if (NULL == prompt) prompt = empty;
printf("\nHit return for next test: %s", prompt);
fflush(stdout);
flushln(stdin);
} /* nexttest */

/* ============================== */
/* Display a value and its length */
static void show(char *caption, int sz, char *stg)
{

if ((unsigned)sz != strlen(stg))
printf("Something is wrong with the sz value\n");
printf("%s: sz = %2d \"%s\"\n", caption, sz, stg);
} /* show */

/* =========== */
/* exercise it */
int main(void)
{
#define LGH 40
#define VALUE 1234567890

char stg[LGH];
unsigned int base;
int sz;

printf("\nExercising basedisplay routine\n");
printf("\nbase sz value\n");
for (base = 2; base <= 16; base++) {
sz = (int)basedisplay(VALUE, base, stg, LGH - 1);
printf("%2d %2d %s\n", base, sz, stg);
}

nexttest("ULONG_MAX");
for (base = 8; base <= 16; base++) {
sz = (int)basedisplay(ULONG_MAX, base, stg, LGH - 1);
printf("%2d %2d %s\n", base, sz, stg);
}

basedisplay(0, 10, stg, 3);
printf("\nzero %s\n", stg);

basedisplay(VALUE, 10, stg, 3);
printf("3 lsdigits only, base 10 %s\n", stg);

printf("\nBad calls:\n");

sz = (int)basedisplay(VALUE, 10, stg, 0);
show("0 length field", sz, stg);

sz = (int)basedisplay(VALUE, 1, stg, 20);
show("base 1, lgh 20", sz, stg);

sz = (int)basedisplay(VALUE, 0, stg, 20);
show("base 0, lgh 20", sz, stg);

sz = (int)signbasedisplay(-1234, 10, stg, 0);
show("0 lgh fld, -ve", sz, stg);

sz = (int)signbasedisplay(-1234, 10, stg, 2);
show("truncate -1234", sz, stg);

nexttest("System limits");

sz = (int)signbasedisplay(SCHAR_MIN, 10, stg, 20);
show("SCHAR_MIN ", sz, stg);

sz = (int)signbasedisplay(SCHAR_MAX, 10, stg, 20);
show("SCHAR_MAX ", sz, stg);

sz = (int)signbasedisplay(UCHAR_MAX, 10, stg, 20);
show("UCHAR_MAX ", sz, stg);

sz = (int)signbasedisplay(CHAR_MIN, 10, stg, 20);
show("CHAR_MIN ", sz, stg);

sz = (int)signbasedisplay(CHAR_MAX, 10, stg, 20);
show("CHAR_MAX ", sz, stg);

sz = (int)signbasedisplay(MB_LEN_MAX, 10, stg, 20);
show("MB_LEN_MAX ", sz, stg);

sz = (int)signbasedisplay(SHRT_MIN, 10, stg, 20);
show("SHRT_MIN ", sz, stg);

sz = (int)signbasedisplay(SHRT_MAX, 10, stg, 20);
show("SHRT_MAX ", sz, stg);

sz = (int)signbasedisplay(USHRT_MAX, 10, stg, 20);
show("USHRT_MAX ", sz, stg);

sz = (int)signbasedisplay(INT_MIN, 10, stg, 20);
show("INT_MIN ", sz, stg);

sz = (int)signbasedisplay(INT_MAX, 10, stg, 20);
show("INT_MAX ", sz, stg);

sz = (int)signbasedisplay(INT_MAX, 10, stg, 20);
show("INT_MAX ", sz, stg);

sz = (int) basedisplay(UINT_MAX, 10, stg, 20);
show("UINT_MAX ", sz, stg);

sz = (int)signbasedisplay(LONG_MIN, 10, stg, 20);
show("LONG_MIN ", sz, stg);

sz = (int)signbasedisplay(LONG_MAX, 10, stg, 20);
show("LONG_MAX ", sz, stg);

sz = (int) basedisplay(ULONG_MAX, 10, stg, 20);
show("ULONG_MAX ", sz, stg);

nexttest("DONE");
return 0;
} /* main */

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Feb 26 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.