Bitwise AND etc

M

mdh

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.
Thanks in advance.
 
I

Ian Collins

mdh said:
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.
Thanks in advance.
0177 is an octal (base 8) number, so it is 01111111 in binary.
 
O

osmium

mdh said:
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.
 
M

Martin Ambuhl

mdh said:
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
 
K

Keith Thompson

Martin Ambuhl said:
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.
 
R

Rudolf

Martin Ambuhl said:
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)
 
R

Rod Pemberton

mdh said:
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.
Thanks in advance.

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
 
K

Keith Thompson

Rod Pemberton said:
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
 
R

Rod Pemberton

Keith Thompson said:
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
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Rod said:
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+6npigBKoXwCeJk5N
9TY1nxlUppWuuJUK7VO1//U=
=7FzG
-----END PGP SIGNATURE-----
 
R

Richard Heathfield

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?
 
J

jjf

Richard said:
Lew Pitcher said:


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

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Richard said:
Lew Pitcher said:


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+SeZm1PyxPGIbrVACgrU9P
gt+zgMkUbU2phDYP3HLooWU=
=qf+d
-----END PGP SIGNATURE-----
 
J

John Devereux

Lew Pitcher said:
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.

I think your sig separator needs an extra " ".
 
P

Peter Shaggy Haywood

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?

How about triptet?

--

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

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top