COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### bitwise operators

• Email
• Follow

```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;

}

All cases work except for the below.

Test isNonZero(-2147483648[0x80000000]) failed.
Gives 0[0x0]. Should be 1[0x1]

If tried everything I could think of and I am all out of ideas. Any one
have any idea how I could fix my code to deal with this negative number?

I also need to write one that will check whether x is nonzero using only ~
& ^ | + << >>

My code so far is:
int isLess(int x, int y) {

int a =0;

a = x ^ y;
a = y >> a;
a = a + 1;
a  = !a;
return !a;
}

Which only works for about half the cases (on small non-negative numbers)

Any input would be appreciated.
Thanks,

```
 0

See related articles to this posting

```77scrapper77 wrote:
>
> 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;

Undefined behaviour for most values of x. The Standard has this to

"If the value of the right operand is negative or is greater than
or equal to the width in bits of the promoted left operand, the
behavior is undefined."

Also, = is an operator, and it's not in your list of allowed operators.

Personally, I don't think it's possible in correct C with just the
operators that you are allowed to use (although I am ready to be
proved wrong).
```
 0
Reply infobahn (503) 2/20/2005 6:25:39 PM

```77scrapper77 wrote:
> 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) {
This function name invades the implementation's namespace
as it starts with "is". Make that IsNonZero().
>
>
> x = (1 << x) & x;

Undefined behaviour, see infobahn's message.

> x = ~x;
> x = x & 1;
> return x;
>
>
> }
>
> All cases work except for the below.

Nope: x=0 ... (~((1<<0) & 0)) & 1 == 1.
So your most important case does not work.

I do not see at all how you can do anything sensible for signed
integers as they could have negative zeros for 1s complement and
sign-magnitude representations. Seems to be a completely hopeless
task. Even for 2s complement only, I do not have the least clue
how to do it.

> Test isNonZero(-2147483648[0x80000000]) failed.
> Gives 0[0x0]. Should be 1[0x1]
>
> If tried everything I could think of and I am all out of ideas. Any one
> have any idea how I could fix my code to deal with this negative number?

No. Not at all.

> I also need to write one that will check whether x is nonzero using only ~

ITYM whether x<y

> & ^ | + << >>
>
> My code so far is:
> int isLess(int x, int y) {

Again: The function name.

> int a =0;
>
> a = x ^ y;
> a = y >> a;

You seem to be completely clueless what the shift operators are doing.

> a = a + 1;
> a  = !a;
>   return !a;

! is an operator, too, and not on your list.
If it was not, your IsNonZero() function's body would consist of
{return !!x;}

> }
>
> Which only works for about half the cases (on small non-negative numbers)
>
> Any input would be appreciated.

Are you allowed to use if, else, ...?
Either you are not telling us the whole truth or you should look
for someone different to give you C problems.

Cheers
Michael
--
E-Mail: Mine is an   /at/ gmx /dot/ de   address.
```
 0
Reply Michael.Mair (1492) 2/20/2005 6:55:08 PM

```"77scrapper77" <gregarious_girl77@nospam.hotmail.com> writes:

> 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.

int is_nonzero (int x)
{
switch (x) {
case 0:
return 0;
default:
return 1;
}
}
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
```
 0
Reply blp (3955) 2/20/2005 7:39:13 PM

```77scrapper77 wrote:
> 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.
>

int isNonZero(int x)
{
if (x)
return 1;
return 0;
}

```
 0
Reply oldwolf (2278) 2/20/2005 8:36:44 PM

```I can't use any loops or if statements. I can use = though.

```
 0

```int isNonZero(int x) {
return(x || 0);
}

```
 0
Reply mfag (26) 2/20/2005 9:26:48 PM

```On Sun, 20 Feb 2005 19:55:08 +0100, Michael Mair
<Michael.Mair@invalid.invalid> wrote:

> 77scrapper77 wrote:
>> 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) {
> This function name invades the implementation's namespace
> as it starts with "is". Make that IsNonZero().

Not true.  From section 7.26 "Future Library Directions":

7.26.2 Character handling <ctype.h>

1 Function names that begin with either is or to, and a lowercase

It is allowed to start an identifier with is and an uppercase letter,
which is what he did.  The same is true as far as I can see with most of
the other namespace restrictions (function names starting with str, mem
or wcs ans a lowercase letter are banned, ones with an uppercase letter
are permitted).

> Are you allowed to use if, else, ...?
> Either you are not telling us the whole truth or you should look
> for someone different to give you C problems.

It looks like homework to me...

Chris C
```
 0
Reply chris23 (646) 2/20/2005 9:31:39 PM

```I can't use || either, just |.

```
 0

```Chris Croughton <chris@keristor.net> writes:

> <Michael.Mair@invalid.invalid> wrote:
>> 77scrapper77 wrote:
>>> int isNonZero(int x) {
>> This function name invades the implementation's namespace
>> as it starts with "is". Make that IsNonZero().
>
> Not true.  From section 7.26 "Future Library Directions":
>
>   7.26.2 Character handling <ctype.h>
>
>   1 Function names that begin with either is or to, and a lowercase
>     letter may be added to the declarations in the <ctype.h> header.

However, C89 implementations are allowed to have a
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
```
 0
Reply blp (3955) 2/20/2005 10:16:55 PM

```On 2005-02-20 12:45:49 -0500, "77scrapper77"
<gregarious_girl77@nospam.hotmail.com> 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.

In C99:

int isNonZero(int x)
{
_Bool b = x;
return b;
}

--
Clark S. Cox, III
clarkcox3@gmail.com

```
 0
Reply clarkcox3 (505) 2/20/2005 10:51:53 PM

```Michael Mair wrote:
> ! is an operator, too, and not on your list.
> If it was not, your IsNonZero() function's body would consist of
> {return !!x;}
>
>> }

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 ;
```
 0
Reply spacenjasset1 (40) 2/20/2005 11:39:53 PM

```I can't use the ! operator.

```
 0

```"77scrapper77" <gregarious_girl77@nospam.hotmail.com> writes:
> I can't use any loops or if statements. I can use = though.

Then you should have said so in the first place.  Old Wolf's solution
satisfied the constraints you originally posted.  You can't expect
much help if you change the problem statement.

The problem statement doesn't look like anything even vaguely useful.
Is it a homework assignment?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
```
 0
Reply kst-u (21963) 2/21/2005 2:59:26 AM

```77scrapper77 wrote:
> 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.

Well, hopefully this hint will not be enough that you don't still have
to do some thinking:

0x0 + 0x3 = 0x3 (011b)
0x1 + 0x3 = 0x3 (100b)
0x2 + 0x3 = 0x3 (101b)
0x3 + 0x3 = 0x3 (110b)

-Chris

```
 0
Reply thesagerat (67) 2/21/2005 6:07:46 AM

```Chris Williams wrote:
>
> 77scrapper77 wrote:
> > 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.
>
> Well, hopefully this hint will not be enough that you don't still have
> to do some thinking:
>
> 0x0 + 0x3 = 0x3 (011b)
> 0x1 + 0x3 = 0x3 (100b)
> 0x2 + 0x3 = 0x3 (101b)
> 0x3 + 0x3 = 0x3 (110b)

But it has to work for /any/ int. Consider the case where x is
equal to INT_MAX. Adding any non-negative value to x would then
result in undefined behaviour (integer overflow).
```
 0
Reply infobahn (503) 2/21/2005 7:03:01 AM

```> Chris Williams wrote:
> > Well, hopefully this hint will not be enough that you don't still
have
> > to do some thinking:
> >
> > 0x0 + 0x3 = 0x3 (011b)
> > 0x1 + 0x3 = 0x4 (100b)
> > 0x2 + 0x3 = 0x5 (101b)
> > 0x3 + 0x3 = 0x6 (110b)

[corrected above table]

infobahn wrote:
> But it has to work for /any/ int. Consider the case where x is
> equal to INT_MAX. Adding any non-negative value to x would then
> result in undefined behaviour (integer overflow).

True, I was assuming a machine where INT_MAX + 1 == INT_MIN, which is
unsafe given a wide enough a range of platforms. Although you can get
around this by only testing up to INT_MAX >> 1 and then using secret,
magic bit twiddling for everything else.
However, spending a few minutes trying to solve this with completely
portable code (e.g. CHAR_BIT could equal 7) but without * / or -, I can
say that undefined behavior is the least of ones worries. I think I can
determine how many bits are in an int, but if I then want to do a right
shift by one less than the total bit count, I am at a loss how to do it
without minus.

int uintBits =  /*I would have to assume there is a better way, but for
the moment brute force it is */
/* One byte's worth of bits */
CHAR_BIT +

/* C will treat CHAR_BIT as an integer, so by shifting left a bytes
worth and back we will either get CHAR_BIT or 0 dependent on whether
the platform supoorts two byte ints */
((CHAR_BIT << CHAR_BIT) >> CHAR_BIT) +

/* If we can move over two then we can add on two more byte's worth
since an int will be either two or four bytes not three (so far as I am
aware) */
(((CHAR_BIT << (CHAR_BIT << 1)) >> (CHAR_BIT << 1)) << 1)
;
int highBit = value >> (uintBits - 1);  //but no minus allowed!!

-Chris

```
 0
Reply thesagerat (67) 2/21/2005 1:35:37 PM

```infobahn <infobahn@btinternet.com> spoke thus:

> Personally, I don't think it's possible in correct C with just the
> operators that you are allowed to use (although I am ready to be
> proved wrong).

I'm sure there's something wrong with

(x<<1)==(x>>1);

but I'm not exactly sure what.

--
Christopher Benson-Manica  | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org    | don't, I need to know.  Flames welcome.
```
 0
Reply ataru (1609) 2/21/2005 2:48:26 PM

```Christopher Benson-Manica wrote:
>
> infobahn <infobahn@btinternet.com> spoke thus:
>
> > Personally, I don't think it's possible in correct C with just the
> > operators that you are allowed to use (although I am ready to be
> > proved wrong).
>
> I'm sure there's something wrong with
>
> (x<<1)==(x>>1);
>
> but I'm not exactly sure what.

The == is wrong, because it's not in the list of allowed operators.

Nice try, though.
```
 0
Reply infobahn (503) 2/21/2005 3:13:15 PM

```Chris Williams wrote:
>
> > Chris Williams wrote:
> > > Well, hopefully this hint will not be enough that you don't still
> have
> > > to do some thinking:
> > >
> > > 0x0 + 0x3 = 0x3 (011b)
> > > 0x1 + 0x3 = 0x4 (100b)
> > > 0x2 + 0x3 = 0x5 (101b)
> > > 0x3 + 0x3 = 0x6 (110b)
>
> [corrected above table]
>
> infobahn wrote:
> > But it has to work for /any/ int. Consider the case where x is
> > equal to INT_MAX. Adding any non-negative value to x would then
> > result in undefined behaviour (integer overflow).

s/non-negative/positive/   (oops)

<snip>
>
>    /* If we can move over two then we can add on two more byte's worth
> since an int will be either two or four bytes not three (so far as I am
> aware) */

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, and I know of at least one system with 8-bit bytes.
```
 0
Reply infobahn (503) 2/21/2005 3:18:12 PM

```77scrapper77 wrote:
> 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.

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
```
 0
Reply news11233 (19) 2/22/2005 2:53:50 AM

```>   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
```
 0
Reply news11233 (19) 2/22/2005 2:56:13 AM

```infobahn wrote:
> Chris Williams wrote:
> >    /* If we can move over two then we can add on two more byte's
worth
> > since an int will be either two or four bytes not three (so far as
I am
> > aware) */
>
> 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

```
 0
Reply thesagerat (67) 2/22/2005 3:01:06 AM

```roman ziak wrote:
>>   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 :)
```
 0
Reply news11233 (19) 2/22/2005 3:05:08 AM

```Chris Williams wrote:
>
> infobahn wrote:
> >
> > 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?

32, in my case.

>
> > and I know of at least one system with 8-bit bytes.
>
> 8-bit bytes? x86?

Bigtime typo. I meant 8-byte ints.
```
 0
Reply infobahn (503) 2/22/2005 4:12:36 AM

```In article <42191fc9@x-privat.org>, spacenjasset@yahoo.co.uk says...
> Michael Mair wrote:
> > ! is an operator, too, and not on your list.
> > If it was not, your IsNonZero() function's body would consist of
> > {return !!x;}
> >
> >> }
>
> 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?

--
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
```
 0
Reply randyhoward (4848) 2/23/2005 4:40:17 AM

```Randy Howard wrote:
>
> 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.
```
 0
Reply infobahn (503) 2/23/2005 7:12:05 AM

```77scrapper77 wrote:
> 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
mfhaigh@sbcglobal.net

```
 0
Reply mfhaigh (154) 2/23/2005 8:06:45 PM

```"Mark F. Haigh" wrote:
>
> 77scrapper77 wrote:
> > 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.

Even if we take C99 for granted (and that's a big if), what if
UINT_MAX < 0x80000000u ?
```
 0
Reply infobahn (503) 2/23/2005 9:02:32 PM

```infobahn wrote:
> "Mark F. Haigh" wrote:
>
>>77scrapper77 wrote:
>>
>>>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.

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
--
E-Mail: Mine is an   /at/ gmx /dot/ de   address.
```
 0
Reply Michael.Mair (1492) 2/23/2005 9:31:03 PM

```infobahn wrote:
> "Mark F. Haigh" wrote:
> >
> > 77scrapper77 wrote:
> > > 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.
>
> 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
mfhaigh@sbcglobal.net

```
 0
Reply mfhaigh (154) 2/23/2005 9:56:54 PM

```Michael Mair wrote:
>
> infobahn wrote:
> > "Mark F. Haigh" wrote:
> >
> >>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.
>
> C99 does not guarantee the exact width integer types.

And in any case, the OP specified int, not uint32_t
```
 0
Reply infobahn (503) 2/23/2005 10:53:43 PM

31 Replies
58 Views

Similar Articles

12/12/2013 11:29:46 PM
[PageSpeed]

Similar Artilces:

Bitwise Operations
On Python3, how can I perform bitwise operations? For instance, I want something that will 'and', 'or', and 'xor' a binary integer. Mahalo, DevynCJohnson@Gmail.com On 2013-07-29, Devyn Collier Johnson <devyncjohnson@gmail.com> wrote: > On Python3, how can I perform bitwise operations? For instance, I want > something that will 'and', 'or', and 'xor' a binary integer. http://www.google.com/search?q=python+bitwise+operations -- Grant Edwards grant.b.edwards Yow! I have the power to ...

Bitwise Operation
Hello, in my little programm: public class Shift1 { public static void main (String [] args) { int thirteen = 13; int ten; ten = thirteen << 1; System.out.println ( thirteen + " is now " + ten); } } I expect 13 (1101) to be shifted by one position to the left, i.e. becoming 101. I expected Java to fill the righthand of the bit sequence with a 0, thus 1010, which should return me 10. It returns 6 instead. What I wrong here? Bye Pasquale -- ======= "I'd like to buy Windows." "Are you mad?" "Why, is this part of the license agreement...

Bitwise operation
Hi, I want to manipulate an incoming WORD as I receive byte by byte. let say 07 D4, which is 2004 I want to combine these two bytes and convert into decimal, then convert as a string. (sprintf) int MSB; int LSB; int Total; char buf[5]; MSB = (0x07 << 8) LSB= (0xD4 & 0x00FF) Total = MSB | LSB sprintf(buf, "%d",Total); When I print buf in %s format, the output I expected is 2004. But it prints "-long printf)d" What is wrong in my code above ? Thanks. On Fri, 30 Jul 2004 10:11:00 +0800, "Magix" <magix@asia.com> wrote in comp.lang.c: > Hi,...

Bitwise Operator
Hello All, I was trying to learn bitwise operator in C .In that process I cam across one resource as mentioned bellow http://graphics.stanford.edu/~seander/bithacks.html#OperationCounting I have one question regarding the section "Compute the minimum (min) or maximum (max) of two integers without branching" In this section author is indicating "if x < y, then -(x < y) will be all ones" . My understanding is if negative number is represented in two's complement method then the above statement is true not for other representation . And C standard does not re...

Bitwise operators for bytea
Is there any particular reason there are no bitwise operators (I'm looking for XOR, specifically) for the bytea data type? Any plans to add them? What I'm trying to do is store an SHA-1 hash in the database, and create a trigger that runs on insert or update and updates a 'running hash' record in another table by xoring it with the new (and in the case of an update, old) hash. The best candidates are bytea, but that has no xor operation, or bit(n), but there's no way to convert back to hexadecimal from it. Also, is there any particular reason sha1() is still no...

Bitwise Operators: Aplication
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting it. I have searched around and really haven't found anything that gave explanation to why these were necessary to learn or even adequately describe what they do. Therefor any explanation or recommendation of the bitwise operators...

Bitwise operator
I'm trying to get the network number given an IP address and subnet mask on the command line Why doesnt this work? #!/usr/local/bin/perl # our (\$ip,\$netmask,@ip,@net,@nw); \$ip=\$ARGV[0]; \$netmask=\$ARGV[1]; @ip=(split(/\./,\$ip)); @net=(split(/\./,\$netmask)); \$i=0; foreach (@ip) { \$nw[\$i]=\$_ & \$net[\$i]; \$i++; } print "IP: \$ip NETMASK: \$netmask NETWORK: @nw\n"; # ./shownet 136.19.96.178 255.255.254.0 IP: 136.19.96.178 NETMASK: 255.255.254.0 NETWORK: 014 01 04 0 Yet 136 & 255 on the command line yields the correct answer # perl print 136 & 255 136...

Practical usage of bitwise operators.
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32 programming in straight C, the Win32 API sends windows messages to applications with various settings stored in bit fields, in which case changing the settings of the message is a matter of using the bitwise operators. This was a good practical example of using bitwise operators. However, in all of my programming endevours and studies (including the K&R book, Data Structures books, Stroustr...

Equivalent of Javascript Bitwise Operators
Please pardon my ignorance on this one - but I'm not certain how the sign bt is treated in python bitwise operators. I've trying to convert a javascript DES encryption routine into python. Javascritp has >>> and >>. >>> is a zero fill bit shift whereas >> is a sign propagating bit shift. My understanding is that the python >> is equivalent to the javascript >> - but python has no equivalent to >>>. Would a >>> 3 in javascript be equivalent to abs(a) >> 3 in python (make sure the sign bit is set to zero first) ? In act...

how to multiply two integers using bitwise operators
How to mulitply two integer numbers using bitwise operators in C language.Please reply as early as possible sandy_pt_in@yahoo.com wrote: > How to mulitply two integer numbers using bitwise operators in C > language.Please reply as early as possible You do know how to sum them, right? Well check this out: 3*4 == 3+3+3+3 3*4 == 4+4+4 <sandy_pt_in@yahoo.com> wrote : > How to mulitply two integer numbers using bitwise operators in C > language.Please reply as early as possible Emulate the way you learned to do it for decimal numbers in elementary school. On Sun, 21 Sep...

How do you bitwise operations in Ada '83 and '95
Also, do you have small coding examples that demonstrate this? Thank you, Christopher Lusardi "Chris L" <clusardi2k@aol.com> writes: > Also, do you have small coding examples that demonstrate this? If you just want bitwise logical operators, then you want an array of Boolean. Probably with pragma Pack. This works in Ada 83, 95, and 05. If you want to mix bitwise logical operators with arithmetic and the like, then you want to use modular types, which are not available in Ada 83. - Bob Chris L wrote: > Also, do you have small coding examples that demonstrate this...

~ operator not operating as expected
I need a bool to toggle between true and false every time it is called - easy I thought bool flag = false // ie 0 .... cout << (int)flag << "\n"; flag = ~flag;. .... what I 0 1 1 1 1 OK I can implement the functionality with a couple of if's but I thought the above was more elegant and am mystified as to why it fails, Can anyone enlighten me ? Dave -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] jk@pusspaws.net wrote: > I need a bool to toggle between true and false...

overloaded * operator and *= operator
Can anyone help me complete this program? It is almost finished except for the multiplication of Polynomials algorithms. I can't seem to correctly implement the process. Thanks. #include < iostream.h > #include < iomanip.h > class Polynomial { friend istream &operator>>( istream &, Polynomial & ); friend ostream &operator<<( istream &, Polynomial & ); public: Polynomial::Polynomial(); Polynomial operator+(Polynomial &); Polynomial operator-( Polynomial &); Polynomial operator+=(Po...

Operator new and new operator
How to oveload "operator new", if it's possible (that one that only allocates memory)? And when I overload "new operator" shoud I always use the parameter size_t (the first one) and when I call my overloaded operator without the paramer, will it allocate the memory itself :-))? ...

Q: operator void* or operator bool?
Hi, I remember reading a document that advised to prefer 'operator void*' over 'operator bool' or other way round, when I want to provide the ability to use code like this: test_class t; while (t) { // do stuff }; But I cannot remember why and which method was prefered. Thanks for the help! -- jb (replace y with x if you want to reply by e-mail) On Fri, 5 Mar 2004 15:50:45 +0100, "Jakob Bieling" <netsurf@gmy.net> wrote: >Hi, > > I remember reading a document that advised to prefer 'operator void*' >over 'operator ...