468,505 Members | 1,756 Online

# Bitwise AND etc

If one searches the forum for bitwise operators there are numerous
questions and very good answers to the perenial question about what
these operators do and why we need them.
So, having looked at all these, I approached K&R 2.9 headily!! ...but
not for long :-(

Quote:
The bitwise AND operator & is often used to mask off some set of bits;
for example,
n=n & 0177;
sets to zero all but the low-order 7 bits of n.

End Quote;

Could someone perhaps just demonstrate this with an example? (I have
tried to make sense of the fact that 0177 is, I believe '10110001'.
However this has not gotten me very far.

May 10 '06 #1
17 3056
mdh wrote:
If one searches the forum for bitwise operators there are numerous
questions and very good answers to the perenial question about what
these operators do and why we need them.
So, having looked at all these, I approached K&R 2.9 headily!! ...but
not for long :-(

Quote:
The bitwise AND operator & is often used to mask off some set of bits;
for example,
n=n & 0177;
sets to zero all but the low-order 7 bits of n.

End Quote;

Could someone perhaps just demonstrate this with an example? (I have
tried to make sense of the fact that 0177 is, I believe '10110001'.
However this has not gotten me very far.

0177 is an octal (base 8) number, so it is 01111111 in binary.

--
Ian Collins.
May 10 '06 #2
"mdh" writes:
The bitwise AND operator & is often used to mask off some set of bits;
for example,
n=n & 0177;
sets to zero all but the low-order 7 bits of n.

End Quote;

Could someone perhaps just demonstrate this with an example? (I have
tried to make sense of the fact that 0177 is, I believe '10110001'.
However this has not gotten me very far.

It's octal so it means 1 111 111.
May 10 '06 #3

Ian Collins wrote:

0177 is an octal (base 8) number, so it is 01111111 in binary.

oops...

Well, I am glad I asked!! Ok...now it makes sense.
thank you.

May 10 '06 #4

osmium wrote:
It's octal so it means 1 111 111.

Thank you.

May 10 '06 #5
mdh wrote:
If one searches the forum for bitwise operators there are numerous
questions and very good answers to the perenial question about what
these operators do and why we need them.
So, having looked at all these, I approached K&R 2.9 headily!! ...but
not for long :-(

Quote:
The bitwise AND operator & is often used to mask off some set of bits;
for example,
n=n & 0177;
sets to zero all but the low-order 7 bits of n.

End Quote;

Could someone perhaps just demonstrate this with an example? (I have
tried to make sense of the fact that 0177 is, I believe '10110001'.

Why you would think that 0177 represents that bit pattern eludes me.
The number 7 (octal, decimal, or hex) is 111 binary.
The octal number 0177 is, quite obviously, 01111111 binary.
At the risk of appearing to belabor the obvious, I'll point out the
grouping:
0(flagging as octal) 1 7 7
binary equivalents: 001 111 111
May 10 '06 #6
Martin Ambuhl <ma*****@earthlink.net> writes:
mdh wrote:

[snip]
Could someone perhaps just demonstrate this with an example? (I have
tried to make sense of the fact that 0177 is, I believe '10110001'.

Why you would think that 0177 represents that bit pattern eludes me.

He undoubtedly didn't realize that the leading 0 makes it an octal
constant. 177 decimal is 10110001 binary.

--
Keith Thompson (The_Other_Keith) ks***@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.
May 10 '06 #7
Martin Ambuhl <ma*****@earthlink.net> wrote:
mdh wrote:
If one searches the forum for bitwise operators there are numerous
questions and very good answers to the perenial question about what
these operators do and why we need them.
So, having looked at all these, I approached K&R 2.9 headily!! ...but
not for long :-(

Quote:
The bitwise AND operator & is often used to mask off some set of bits;
for example,
n=n & 0177;
sets to zero all but the low-order 7 bits of n.

End Quote;

Could someone perhaps just demonstrate this with an example? (I have
tried to make sense of the fact that 0177 is, I believe '10110001'.

Why you would think that 0177 represents that bit pattern eludes me.

The OP clearly thought that 0177 was a base 10 number.

177 (base 10) == 0xB1 (base 16) == 10110001 (base 2)
May 10 '06 #8

"mdh" <md**@comcast.net> wrote in message
If one searches the forum for bitwise operators there are numerous
questions and very good answers to the perenial question about what
these operators do and why we need them.
So, having looked at all these, I approached K&R 2.9 headily!! ...but
not for long :-(

Quote:
The bitwise AND operator & is often used to mask off some set of bits;
for example,
n=n & 0177;
sets to zero all but the low-order 7 bits of n.

End Quote;

Could someone perhaps just demonstrate this with an example? (I have
tried to make sense of the fact that 0177 is, I believe '10110001'.
However this has not gotten me very far.

Others have pointed out that the conversion is incorrect. My advice is to
learn hexadecimal since it is easily converted to binary. If you need to
use octal or decimal, get a calculator to convert to hex or binary.
Rod Pemberton
May 11 '06 #9
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
"mdh" <md**@comcast.net> wrote in message
If one searches the forum for bitwise operators there are numerous
questions and very good answers to the perenial question about what
these operators do and why we need them.
So, having looked at all these, I approached K&R 2.9 headily!! ...but
not for long :-(

Quote:
The bitwise AND operator & is often used to mask off some set of bits;
for example,
n=n & 0177;
sets to zero all but the low-order 7 bits of n.

End Quote;

Could someone perhaps just demonstrate this with an example? (I have
tried to make sense of the fact that 0177 is, I believe '10110001'.
However this has not gotten me very far.

Others have pointed out that the conversion is incorrect. My advice is to
learn hexadecimal since it is easily converted to binary. If you need to
use octal or decimal, get a calculator to convert to hex or binary.

octal is even easier to convert to binary than hexadecimal is.

1 -> 001
7 -> 111
0177 -> 001 111 111

--
Keith Thompson (The_Other_Keith) ks***@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.
May 11 '06 #10

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmm> writes:
"mdh" <md**@comcast.net> wrote in message
If one searches the forum for bitwise operators there are numerous
questions and very good answers to the perenial question about what
these operators do and why we need them.
So, having looked at all these, I approached K&R 2.9 headily!! ...but
not for long :-(

Quote:
The bitwise AND operator & is often used to mask off some set of bits;
for example,
n=n & 0177;
sets to zero all but the low-order 7 bits of n.

End Quote;

Could someone perhaps just demonstrate this with an example? (I have
tried to make sense of the fact that 0177 is, I believe '10110001'.
However this has not gotten me very far.

Others have pointed out that the conversion is incorrect. My advice is to learn hexadecimal since it is easily converted to binary. If you need to use octal or decimal, get a calculator to convert to hex or binary.

octal is even easier to convert to binary than hexadecimal is.

1 -> 001
7 -> 111
0177 -> 001 111 111

It simply doesn't fit nicely within a multiple of a nybble.

Rod Pemberton
May 12 '06 #11
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rod Pemberton wrote:
"Keith Thompson" <ks***@mib.org> wrote in message

[snip]
octal is even easier to convert to binary than hexadecimal is.

1 -> 001
7 -> 111
0177 -> 001 111 111

It simply doesn't fit nicely within a multiple of a nybble.

But, for those machines that use 9bit bytes, octal is ideal. You get three
octets per byte :-)

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEZR7cagVFX4UWr64RAoEjAKCz6osDvQUdwRcxPnT+6n pigBKoXwCeJk5N
9TY1nxlUppWuuJUK7VO1//U=
=7FzG
-----END PGP SIGNATURE-----
May 13 '06 #12
Lew Pitcher said:
But, for those machines that use 9bit bytes, octal is ideal. You get three
octets per byte :-)

I'm not quite sure what you mean here, but you don't mean octets. Three
octets is 24 bits, which would be - er - 2.66+ 9-bit bytes.

Perhaps you mean - well, not nybbles exactly... Trybbles, perhaps?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 13 '06 #13

Richard Heathfield wrote:
Lew Pitcher said:
But, for those machines that use 9bit bytes, octal is ideal. You get three
octets per byte :-)

I'm not quite sure what you mean here, but you don't mean octets. Three
octets is 24 bits, which would be - er - 2.66+ 9-bit bytes.

Perhaps you mean - well, not nybbles exactly... Trybbles, perhaps?

And we all know that trybbles cause nothing but trouble ...

May 13 '06 #14
jj*@bcs.org.uk writes:
Richard Heathfield wrote:
Lew Pitcher said:
> But, for those machines that use 9bit bytes, octal is ideal. You get three
> octets per byte :-)

I'm not quite sure what you mean here, but you don't mean octets. Three
octets is 24 bits, which would be - er - 2.66+ 9-bit bytes.

Perhaps you mean - well, not nybbles exactly... Trybbles, perhaps?

And we all know that trybbles cause nothing but trouble ...

Yeah, it's that funky unary multiplication operator that always
confuses people.

--
Keith Thompson (The_Other_Keith) ks***@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.
May 13 '06 #15
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Richard Heathfield wrote:
Lew Pitcher said:
But, for those machines that use 9bit bytes, octal is ideal. You get three
octets per byte :-)

I'm not quite sure what you mean here, but you don't mean octets. Three
octets is 24 bits, which would be - er - 2.66+ 9-bit bytes.

Perhaps you mean - well, not nybbles exactly... Trybbles, perhaps?

Sorry, yes. The proper name escaped me

I meant those three-bit things that can hold one of eight possible values.
"Trybbles", you say? OK, I guess, but you know that I have trouble with
trybbles. They keep multiplying and multiplying and pretty soon, I'm up to my
navel in them.

:-)

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEZUFiagVFX4UWr64RAjwiAJ4reJYoaqWUfg+SeZm1Py xPGIbrVACgrU9P
gt+zgMkUbU2phDYP3HLooWU=
=qf+d
-----END PGP SIGNATURE-----
May 13 '06 #16
Lew Pitcher <lp******@sympatico.ca> writes:
Rod Pemberton wrote:
"Keith Thompson" <ks***@mib.org> wrote in message [snip]
octal is even easier to convert to binary than hexadecimal is.

1 -> 001
7 -> 111
0177 -> 001 111 111

It simply doesn't fit nicely within a multiple of a nybble.

But, for those machines that use 9bit bytes, octal is ideal. You get three
octets per byte :-)

I still find the octal notation useful for numbering things that come
in groups of 8. For example in digital electronics, quite often you
might have, say, 64 outputs arranged in 8 groups of 8 (because the
chips have "byte-wide" busses.. So you can number them in octal, and
know that 023 is output #3 in bank #2. It is nice to have this
supported directly in C.
--
Lew Pitcher

I think your sig separator needs an extra " ".

--

John Devereux
May 13 '06 #17
Groovy hepcat Richard Heathfield was jivin' on Sat, 13 May 2006
00:07:43 +0000 in comp.lang.c.
Re: Bitwise AND etc's a cool scene! Dig it!
Perhaps you mean - well, not nybbles exactly... Trybbles, perhaps?

--

Dig the even newer still, yet more improved, sig!