Discussion:
Failing to grasp bitwise AND operation..
(too old to reply)
Paul Cusbish
2004-08-09 11:00:58 UTC
Permalink
Hello group,

I have a question regarding AND operations.

I am told that to look at the LSB of a word (32bit), I have to AND it
with a value (FF i was told), and
I will be left with the lower byte. The thing is, say I try this
with the number 23; What do I AND it with to get the lower part of
that word??!

your's confused

Paul.
Bjorn van der Meer
2004-08-09 11:42:35 UTC
Permalink
On 9 Aug 2004 04:00:58 -0700
Post by Paul Cusbish
Hello group,
I have a question regarding AND operations.
I am told that to look at the LSB of a word (32bit), I have to AND it
with a value (FF i was told), and
I will be left with the lower byte. The thing is, say I try this
with the number 23; What do I AND it with to get the lower part of
that word??!
your's confused
Paul.
FF in 32-Bit Binary = 00000000 0000000 0000000 11111111
AND
23 in 32-Bit Binary = 00000000 0000000 0000000 00010111
gives you
23 in 32-Bit Binary = 00000000 0000000 0000000 00010111

Better view:
00000000 0000000 0000000 11111111
00000000 0000000 0000000 00010111 AND
--------------------------------------------
00000000 0000000 0000000 00010111



This just discarded all possible "one" in the first three bytes, which you can´t see in this particular example, because with "23" there aren´t any.

Let´s do this instead:

23 in 8 Bit, trying to get the last nibble (4 Bit):

Our bitmask is "F" in 8-Bit binary: 0000 1111
AND
23 in 8 Bit Binary: 0001 0111
gives you 0000 0111

Better view:
0000 1111
0001 0111 AND
------------
0000 0111



You just discarded the "one" at position 5.




Cheers, B.

--
Bjoern van der Meer
Erkrath, Germany
ctr
2004-08-09 16:40:22 UTC
Permalink
Post by Paul Cusbish
Hello group,
I have a question regarding AND operations.
I am told that to look at the LSB of a word (32bit), I have to AND it
with a value (FF i was told), and
I will be left with the lower byte. The thing is, say I try this
with the number 23; What do I AND it with to get the lower part of
that word??!
your's confused
Paul.
well... not quite...
in order to obtain the LSB, you should &(bit and, not logical and) it with 0x01
why? because: 0&?=0, 1&1=1 and for a byte in binary representation,
0b???????x & 0b00000001 = 0b0000000x
i hope it helps...
n***@comcast.net
2004-08-09 16:53:38 UTC
Permalink
Post by ctr
Post by Paul Cusbish
Hello group,
I have a question regarding AND operations.
I am told that to look at the LSB of a word (32bit), I have to AND it
with a value (FF i was told), and
I will be left with the lower byte. The thing is, say I try this
with the number 23; What do I AND it with to get the lower part of
that word??!
your's confused
Paul.
well... not quite...
in order to obtain the LSB, you should &(bit and, not logical and) it with 0x01
why? because: 0&?=0, 1&1=1 and for a byte in binary representation,
0b???????x & 0b00000001 = 0b0000000x
i hope it helps...
LSB (capital letters) usually means "least significant byte", so
ANDing with 0xff is indeed correct. lsb (lower case letters) is
usually used for "least significant bit", in which case you'd AND with
1, as you said.
--
That's News To Me!
***@comcast.net
ctr
2004-08-10 06:44:57 UTC
Permalink
Post by n***@comcast.net
Post by ctr
Post by Paul Cusbish
Hello group,
I have a question regarding AND operations.
I am told that to look at the LSB of a word (32bit), I have to AND it
with a value (FF i was told), and
I will be left with the lower byte. The thing is, say I try this
with the number 23; What do I AND it with to get the lower part of
that word??!
your's confused
Paul.
well... not quite...
in order to obtain the LSB, you should &(bit and, not logical and) it with 0x01
why? because: 0&?=0, 1&1=1 and for a byte in binary representation,
0b???????x & 0b00000001 = 0b0000000x
i hope it helps...
LSB (capital letters) usually means "least significant byte", so
ANDing with 0xff is indeed correct. lsb (lower case letters) is
usually used for "least significant bit", in which case you'd AND with
1, as you said.
lsb, LSB...
we're running low on abbreviations...
Paul Cusbish
2004-08-10 14:34:43 UTC
Permalink
Post by ctr
Post by n***@comcast.net
Post by ctr
Post by Paul Cusbish
Hello group,
I have a question regarding AND operations.
I am told that to look at the LSB of a word (32bit), I have to AND it
with a value (FF i was told), and
I will be left with the lower byte. The thing is, say I try this
with the number 23; What do I AND it with to get the lower part of
that word??!
your's confused
Paul.
well... not quite...
in order to obtain the LSB, you should &(bit and, not logical and) it with 0x01
why? because: 0&?=0, 1&1=1 and for a byte in binary representation,
0b???????x & 0b00000001 = 0b0000000x
i hope it helps...
LSB (capital letters) usually means "least significant byte", so
ANDing with 0xff is indeed correct. lsb (lower case letters) is
usually used for "least significant bit", in which case you'd AND with
1, as you said.
lsb, LSB...
we're running low on abbreviations...
Hi there,

Thanks a bunch for your replies - very good stuff.

So basically, i use an AND to highlight certain bits, for say, testing
purposes, etc?

Is there a guide which details what kind of operations the other
bitwise stuff is useful for?

<<, >>, ~, etc??

Thanks again, your help is much appreciated!

Paul.
John Slimick
2004-08-10 20:16:29 UTC
Permalink
Post by Paul Cusbish
Hi there,
Thanks a bunch for your replies - very good stuff.
So basically, i use an AND to highlight certain bits, for say, testing
purposes, etc?
Is there a guide which details what kind of operations the other
bitwise stuff is useful for?
<<, >>, ~, etc??
Thanks again, your help is much appreciated!
Paul.
Hello:

I don't know of any handbook, but I have seen
the bit operations used as:

(nibble is four bits -- half a byte)

(1) Turning bits off (and with 0) and on (and with 1)
but of course the bit is imbedded in a word
of control and status bits;

(2) Testing bits; for example if we have a byte

10111100

and we want to test bit 3 (from the lsb), we
would and with 00001000 (hex 08) and test
for non zero

(3) Digging bits out of a word; for example,
if we have a byte with

10111100

and we want to dig the four middle bits out,
we would and with 00111100 (hex 3C), and
we would probably want to shift it two
places to the right (>> 2).

(4) Inserting a nibble or some such into a word;
for the byte above, if we wished to insert
an arbitary nibble in that middle position
we could do the following:

1011100 and 11000011 -> 10000000 (word)
nibble << 2 -> nibbler
word or nibbler -> word

(5) The DEC PDP-1 had a slow and clumsy multiplier.
I implemented multiply-by-ten as a series of
shifts and adds. In C this would be

((word << 2) + word)<< 1

left shift (<<) by one is a multiplication by two
right shift (>>) by one is a division by two

In this case we are computing ((word*4)+word)*2

john slimick
***@pitt.edu

Loading...