bitwise operators

R

roman ziak

77scrapper77 said:
hi,

I need to write a function that will check whether x is nonzero. Return 0
if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
<< >> and I am not allowed to use an loops.

My program so far looks like:

int isNonZero(int x) {


x = (1 << x) & x;
x = ~x;
x = x & 1;
return x;


}

I am not sure if standard defines the case when you are shifting left by
more than the bit size of integer.

But how about:

N-1
---
[ \ (x >> i) ] & 1
/
---
i=0

where N is the bit size of word, so for 4 bit machine the expression is:

( x + (x>>1) + (x>>2) + (x>>4) ) & 1
 
R

roman ziak

N-1
---
[ \ (x >> i) ] & 1
/
---
i=0

where N is the bit size of word, so for 4 bit machine the expression is:

( x + (x>>1) + (x>>2) + (x>>4) ) & 1

correction:

( x + (x>>1) + (x>>2) + (x>>3) ) & 1
 
C

Chris Williams

infobahn said:
It must have a sign bit and at least 15 value bits, but it can be
one byte, or two, or three, or four, or five, or eight, or even
thirty-seven. I have worked on systems with 1-, 2-, and 4-byte
ints,

How does one get "a sign bit and at least 15 value bits" with 1-byte
ints? Or is this a platform with 16-bit bytes?
and I know of at least one system with 8-bit bytes.

8-bit bytes? x86?

But back to the drawing board I guess for my bit-count determiner =)

-Chris
 
R

roman ziak

roman said:
N-1
---
[ \ (x >> i) ] & 1
/
---
i=0

where N is the bit size of word, so for 4 bit machine the expression is:

( x + (x>>1) + (x>>2) + (x>>4) ) & 1


correction:

( x + (x>>1) + (x>>2) + (x>>3) ) & 1

sorry, one more correction:

( x | (x>>1) | (x>>2) | (x>>3) ) & 1

this should be the last :)
 
R

Randy Howard

This has it. That's the first thing I thought of. Any excuse to stack up
operators.

If you don't like that then, try return !!!!x; instead


It's best not to use tricks though, readability is paramount.

also, this will suffice, return x ? 1 : 0 ;


Why are doing obvious homework assignments, no matter how trivial?
 
I

infobahn

Randy said:
Why are doing obvious homework assignments, no matter how trivial?

I don't think we did, unless you count "this homework assignment
is impossible" as "doing" the assignment.
 
M

Mark F. Haigh

77scrapper77 said:
hi,

I need to write a function that will check whether x is nonzero. Return 0
if x is = to 0 and 1 otherwise. The open operators I can use are ~ & ^ | +
<< >> and I am not allowed to use an loops.

<snip>


#include <stdio.h>
#include <stdint.h>

int func(uint32_t x)
{
return ~((x - 1) & ~x & 0x80000000u) >> 31;
}


I think this should work. Needs C99 for the exact width unsigned
integer type.


Mark F. Haigh
(e-mail address removed)
 
I

infobahn

Mark F. Haigh said:
<snip>

#include <stdio.h>
#include <stdint.h>

int func(uint32_t x)
{
return ~((x - 1) & ~x & 0x80000000u) >> 31;
}

I think this should work. Needs C99 for the exact width unsigned
integer type.

Even if we take C99 for granted (and that's a big if), what if
UINT_MAX < 0x80000000u ?
 
M

Michael Mair

C99 does not guarantee the exact width integer types.
Even if we take C99 for granted (and that's a big if), what if
UINT_MAX < 0x80000000u ?

You know it, I know it, so let's just sing it together:
0x80000000ul


Cheers
Michael
 
M

Mark F. Haigh

infobahn said:
Even if we take C99 for granted (and that's a big if), what if
UINT_MAX < 0x80000000u ?

Huh? With C99, if 0x80000000u is not representable as an unsigned int,
it becomes a unsigned long.

But it's not really production code we're talking about here, is it?


Mark F. Haigh
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top