By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,037 Members | 2,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,037 IT Pros & Developers. It's quick & easy.

Basic bits operation

P: n/a
Simple question, I think:

I have a known byte, "byte 1":

0000 0100

and a known portion of an unknown byte, "byte 2":

bbbb 0100

I need to compare the lower 4 bits (0100) of each and determine if
"byte 1, lower 4" == "byte 2, lower 4".
What's the most efficient way?

Aug 11 '07 #1
Share this Question
Share on Google+
28 Replies


P: n/a
philbo30 wrote:
Simple question, I think:

I have a known byte, "byte 1":

0000 0100

and a known portion of an unknown byte, "byte 2":

bbbb 0100

I need to compare the lower 4 bits (0100) of each and determine if
"byte 1, lower 4" == "byte 2, lower 4".
What's the most efficient way?
(a&0x0f) == (b&x0f)

--
Ian Collins.
Aug 11 '07 #2

P: n/a
Ian Collins wrote:
philbo30 wrote:
>Simple question, I think:

I have a known byte, "byte 1":

0000 0100

and a known portion of an unknown byte, "byte 2":

bbbb 0100

I need to compare the lower 4 bits (0100) of each and determine if
"byte 1, lower 4" == "byte 2, lower 4".
What's the most efficient way?
(a&0x0f) == (b&x0f)
Oops, (a&0x0f) == (b&0x0f)

--
Ian Collins.
Aug 11 '07 #3

P: n/a
On Sat, 11 Aug 2007 16:01:00 -0700, philbo30 wrote:
Simple question, I think:

I have a known byte, "byte 1":

0000 0100

and a known portion of an unknown byte, "byte 2":

bbbb 0100

I need to compare the lower 4 bits (0100) of each and determine if
"byte 1, lower 4" == "byte 2, lower 4".
What's the most efficient way?
Use byte2 & 15 (or equivalently byte2 % 16, in this case) to
isolate the lower four bits. Then compare the lower four bits of
byte2 with the lower four bits of byte1 (remember that == binds
more tightly than &, so make sure you have enough parentheses), or
directly with 8 if you know that byte1 is always 8.

--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 11 '07 #4

P: n/a
On Sun, 12 Aug 2007 11:13:04 +1200, Ian Collins <ia******@hotmail.com>
wrote:
>Ian Collins wrote:
>philbo30 wrote:
>>Simple question, I think:

I have a known byte, "byte 1":

0000 0100

and a known portion of an unknown byte, "byte 2":

bbbb 0100

I need to compare the lower 4 bits (0100) of each and determine if
"byte 1, lower 4" == "byte 2, lower 4".
What's the most efficient way?
(a&0x0f) == (b&x0f)
Oops, (a&0x0f) == (b&0x0f)
!((a ^ b) & 0xF)

Will also work. Efficiency is architecture dependent, so test for it.

--
Dan Henry
Aug 11 '07 #5

P: n/a
Op Sun, 12 Aug 2007 01:18:29 +0200 schreef Army1987:
On Sat, 11 Aug 2007 16:01:00 -0700, philbo30 wrote:
>Simple question, I think:

I have a known byte, "byte 1":

0000 0100

and a known portion of an unknown byte, "byte 2":

bbbb 0100

I need to compare the lower 4 bits (0100) of each and determine if
"byte 1, lower 4" == "byte 2, lower 4".
What's the most efficient way?
Use byte2 & 15 (or equivalently byte2 % 16, in this case) to
isolate the lower four bits. Then compare the lower four bits of
byte2 with the lower four bits of byte1 (remember that == binds
more tightly than &, so make sure you have enough parentheses), or
directly with 8 if you know that byte1 is always 8.
The byte maybe a signed char. If it holds a negative value, the remainder
after % is negative too ;-) Better use & here.
--
Coos
Aug 11 '07 #6

P: n/a
philbo30 wrote:
>
I have a known byte, "byte 1": and a known portion of an unknown
byte, "byte 2":

I need to compare the lower 4 bits (0100) of each and determine
if "byte 1, lower 4" == "byte 2, lower 4".

What's the most efficient way?
if ((b1 ^ b2) & 0x0f) /* bit fields unequal */ ;
else /* bit fields equal */ ;

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Aug 11 '07 #7

P: n/a
Dan Henry <us****@danlhenry.comwrote:
On Sun, 12 Aug 2007 11:13:04 +1200, Ian Collins <ia******@hotmail.com>
wrote:
philbo30 wrote:
I need to compare the lower 4 bits (0100) of each and determine if
"byte 1, lower 4" == "byte 2, lower 4".
Oops, (a&0x0f) == (b&0x0f)

!((a ^ b) & 0xF)

Will also work. Efficiency is architecture dependent, so test for it.
I'd never use that code unless it were shown with absolute certainty to
be a bottleneck. It's highly unclear compared Ian's. _If_ you can show
that the program is unacceptably slow with Ian's version, and fast
enough with yours, then I'd still first look for a better algorithm; and
only if that cannot be found, I'd use your version, and add a big
comment explaining why I didn't use the obvious code.

Richard
Aug 13 '07 #8

P: n/a
Richard Bos wrote:
Dan Henry <us****@danlhenry.comwrote:
>Ian Collins <ia******@hotmail.comwrote:
>>philbo30 wrote:

I need to compare the lower 4 bits (0100) of each and determine
if "byte 1, lower 4" == "byte 2, lower 4".
>>Oops, (a&0x0f) == (b&0x0f)

!((a ^ b) & 0xF)

Will also work. Efficiency is architecture dependent, so test
for it.

I'd never use that code unless it were shown with absolute
certainty to be a bottleneck. It's highly unclear compared Ian's.
_If_ you can show that the program is unacceptably slow with
Ian's version, and fast enough with yours, then I'd still first
look for a better algorithm; and only if that cannot be found,
I'd use your version, and add a big comment explaining why I
didn't use the obvious code.
What's unclear? The xor isolates the different bits (if any), and
the mask constrains the consideration to the lower 4 bits. It
avoids the non-vanishing probability of the compiler masking both
operands separately. I believe the original post specified that
speed was important.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 14 '07 #9

P: n/a
On Mon, 13 Aug 2007 06:41:33 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
>Dan Henry <us****@danlhenry.comwrote:
>On Sun, 12 Aug 2007 11:13:04 +1200, Ian Collins <ia******@hotmail.com>
wrote:
>philbo30 wrote:
I need to compare the lower 4 bits (0100) of each and determine if
"byte 1, lower 4" == "byte 2, lower 4".
>Oops, (a&0x0f) == (b&0x0f)

!((a ^ b) & 0xF)

Will also work. Efficiency is architecture dependent, so test for it.

I'd never use that code unless it were shown with absolute certainty to
be a bottleneck.
The OP asked "What's the most efficient way?". This alternate way
might be more efficient on some architectures and should be tested to
see.
>It's highly unclear compared Ian's.
It might be unclear to yet-another-general-ledger application writers,
but to folks that work close to the hardware, it is an instantly
recognizable and commonly used expression that certainly does not
merit a "big comment". We use bitwise operators all the time.
>_If_ you can show
that the program is unacceptably slow with Ian's version, and fast
enough with yours, then I'd still first look for a better algorithm; and
only if that cannot be found, I'd use your version, and add a big
comment explaining why I didn't use the obvious code.
Right, you don't like it. I can tell. We travel in different
circles.

--
Dan Henry
Aug 14 '07 #10

P: n/a
On Mon, 13 Aug 2007 06:41:33 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
>Dan Henry <us****@danlhenry.comwrote:
>On Sun, 12 Aug 2007 11:13:04 +1200, Ian Collins <ia******@hotmail.com>
wrote:
>philbo30 wrote:
I need to compare the lower 4 bits (0100) of each and determine if
"byte 1, lower 4" == "byte 2, lower 4".
>Oops, (a&0x0f) == (b&0x0f)

!((a ^ b) & 0xF)

Will also work. Efficiency is architecture dependent, so test for it.

I'd never use that code unless it were shown with absolute certainty to
be a bottleneck.
Me either and then I would. The OP asked for efficiency. I posted an
alternative expression to Ian's. I don't know the OP's architecture
or toolchain. If the OP is concerned about efficiency, let the OP
test it.
>It's highly unclear compared Ian's.
Yes, oh dear, highly obscure, yet it was clear enough for my feeble
brain to recognize as an alternative. Strength in clarity compared to
Ian's was not a consideration for me.
>_If_ you can show
that the program is unacceptably slow with Ian's version, and fast
enough with yours, then I'd still first look for a better algorithm; and
only if that cannot be found, I'd use your version, and add a big
comment explaining why I didn't use the obvious code.
I cannot do that for you and am not particularly motivated to do so. I
don't know the OP's architecture or toolchain. Was that tiny
expression obfuscated to the point of being incomprehensible at a
casual glance? I'd guess that you have run across the '&' operator
before, so was it the '^' (exclusive-or) operator that threw you?

I certainly have no investment in what I posted and simply supplied an
alternative expression. Is that not allowed here? I was kind of
C-like, after all.

--
Dan Henry
Aug 14 '07 #11

P: n/a
Dan Henry <us****@danlhenry.comwrote:
On Mon, 13 Aug 2007 06:41:33 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
_If_ you can show
that the program is unacceptably slow with Ian's version, and fast
enough with yours, then I'd still first look for a better algorithm; and
only if that cannot be found, I'd use your version, and add a big
comment explaining why I didn't use the obvious code.

I cannot do that for you and am not particularly motivated to do so.
You can't do it for anyone but yourself.
I don't know the OP's architecture or toolchain. Was that tiny
expression obfuscated to the point of being incomprehensible at a
casual glance? I'd guess that you have run across the '&' operator
before, so was it the '^' (exclusive-or) operator that threw you?
Let's put it this way. Your line was immediately obvious at a moment's
thought. Ian's line was immediately obvious _without_ having to think
about it. Unless there is clear evidence that your version is more
efficient in a particular situation, I'd prefer not having to think
about individual lines of code, so I can focus my thought on what the
program actually does.

Richard
Aug 15 '07 #12

P: n/a
On Wed, 15 Aug 2007 12:57:04 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
>Dan Henry <us****@danlhenry.comwrote:
>On Mon, 13 Aug 2007 06:41:33 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
I don't know the OP's architecture or toolchain. Was that tiny
expression obfuscated to the point of being incomprehensible at a
casual glance? I'd guess that you have run across the '&' operator
before, so was it the '^' (exclusive-or) operator that threw you?

Let's put it this way. Your line was immediately obvious at a moment's
thought. Ian's line was immediately obvious _without_ having to think
about it.
Too bad for me that the patent period has expired without my having
taken advantage of this (by my 30-year observation) most common form
of I/O port change detection logic. I and those around me *instantly*
recognize the expression (i.e., *without* a moment's thought).

"I pray Thee Lord, from this point forward, I promise to try to be
more tolerant of those who, through no particular fault of their own,
might take pause 'to think about it' when seeing 'my line'. I pray
that you give me strength when seeing 'my line' promulgated
world-wide, but without my benefit. I will strive to keep my wits and
mental balance despite the intolerable anguish of realizing my lost
opportunity by not legitimately claiming it as 'my own'. You tested
Job, now test me. I pray that I remain worthy."

Amen

--
Dan Henry
Aug 17 '07 #13

P: n/a
Dan Henry <us****@danlhenry.comwrote:
Amen
*Plonk*

Richard
Aug 17 '07 #14

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Dan Henry <us****@danlhenry.comwrote:
>Amen

*Plonk*

Richard
All the guy did, with an explanation, was to explain why he gave the
alternate expression.
Aug 17 '07 #15

P: n/a
On Fri, 17 Aug 2007 10:38:52 +0200, Richard <rg****@gmail.comwrote:
>rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>Dan Henry <us****@danlhenry.comwrote:
>>Amen

*Plonk*

All the guy did, with an explanation, was to explain why he gave the
alternate expression.
Alternate expressions seem to be allowed in c.l.c. It could have been
that obscure exclusive-or operator that derailed him. No wait -- I
see that mere moments apart CBFalconer and I both used the offensive
exclusive-or operator, yet CBF got away with it scot-free. Methinks
there is some mighty fishy selective tolerance going on here!

Seriously though, no worries. I think Mr. Bos is better off no longer
reading my posts. My day-to-day use of C for working directly with
hardware exposes me to the kinds of expressions and usage patterns
that he apparently finds to be uncommon. If he were to read my future
posts, we'd likely have similar silly exchanges again. His killfiling
me now will avoid that, so I'm content with the quick action he has
taken.

--
Dan Henry
Aug 21 '07 #16

P: n/a
Dan Henry said:
On Fri, 17 Aug 2007 10:38:52 +0200, Richard <rg****@gmail.comwrote:
>>rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>>Dan Henry <us****@danlhenry.comwrote:

Amen

*Plonk*

All the guy did, with an explanation, was to explain why he gave the
alternate expression.

Alternate expressions seem to be allowed in c.l.c.
Indeed they are.
It could have been
that obscure exclusive-or operator that derailed him.
It will take more than an XOR to derail Richard Bos.
No wait -- I
see that mere moments apart CBFalconer and I both used the offensive
exclusive-or operator, yet CBF got away with it scot-free. Methinks
there is some mighty fishy selective tolerance going on here!
FWIW I had no problem reading your expression (although I must admit I
prefer foo == 0 to !foo). Nor did Richard Bos - you can be sure of
that. He rightly enjoys a great deal of respect in this newsgroup for
his C knowledge, and would have had no trouble working out what your
code means. He just didn't think it was as self-evident as the code
that preceded it in the thread. But Richard Bos doesn't have a monopoly
on what is considered good code. It is purely a style issue, and
opinions on style vary widely within this group.
Seriously though, no worries. I think Mr. Bos is better off no longer
reading my posts.
He plonked you not for your code but for your ill-considered "prayer"
response, which seemed to suggest that you were more interested in
mockery than in thought. I hope that that response was merely a
temporary aberration.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 21 '07 #17

P: n/a
Dan Henry wrote:
>
On Fri, 17 Aug 2007 10:38:52 +0200, Richard <rg****@gmail.comwrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Dan Henry <us****@danlhenry.comwrote:

Amen

*Plonk*
All the guy did, with an explanation, was to explain why he gave the
alternate expression.

Alternate expressions seem to be allowed in c.l.c.
I liked your ^ expresssion good enough.
It had a thoughtful comment about speed to go with it.
I don't consider ^ to be much more cryptic than &.
The thread subject was "Basic bits operation";
I wouldn't expect that to mean "& and nothing else"

But like Mr. Heathfield said,
I think it was your witty sarcasm that got the plonk.

I've indulged in witty sarcasm a lot on this newsgroup,
but I know that it's not good to do.

--
pete
Aug 21 '07 #18

P: n/a
pete said:

<snip>
I've indulged in witty sarcasm a lot on this newsgroup,
Well, half-witty, anyway.

And having failed to resist writing this reply, I will now try very hard
to resist posting it.

--
Rats.
Aug 21 '07 #19

P: n/a
Richard Heathfield wrote:
>
pete said:

<snip>
I've indulged in witty sarcasm a lot on this newsgroup,

Well, half-witty, anyway.

And having failed to resist writing this reply,
I will now try very hard to resist posting it.
My posts that I *have* discarded before sending, oh boy, oh boy...

--
pete
Aug 21 '07 #20

P: n/a
On Tue, 21 Aug 2007 00:24:53 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>Dan Henry said:
>It could have been
that obscure exclusive-or operator that derailed him.

It will take more than an XOR to derail Richard Bos.
...

FWIW I had no problem reading your expression (although I must admit I
prefer foo == 0 to !foo). Nor did Richard Bos - you can be sure of
that. He rightly enjoys a great deal of respect in this newsgroup for
his C knowledge, and would have had no trouble working out what your
code means. He just didn't think it was as self-evident as the code
that preceded it in the thread.
For the sake of accuracy, the phrase he used was "It's highly
unclear..."

FWIW, I've been reading c.l.c for an embarrassingly long time, so I am
not ignorant regarding current participants' level of respect amongst
their peers. I am even aware that there were other knowledgeable
folks that have come and gone before Mr. Bos and some others of the
current crew came on the scene.

--
Dan Henry
Aug 21 '07 #21

P: n/a
On Mon, 20 Aug 2007 20:46:42 -0400, pete <pf*****@mindspring.com>
wrote:
>Dan Henry wrote:
>>
On Fri, 17 Aug 2007 10:38:52 +0200, Richard <rg****@gmail.comwrote:
>rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:

Dan Henry <us****@danlhenry.comwrote:

Amen

*Plonk*

All the guy did, with an explanation, was to explain why he gave the
alternate expression.

Alternate expressions seem to be allowed in c.l.c.

I liked your ^ expresssion good enough.
It had a thoughtful comment about speed to go with it.
That was the OP's criterion ("efficient" actually, not speed). I
presented something different and from the very start advised the OP
to test it. The OP never even defined what he considered efficient;
fewer instructions, faster execution, typing economy, etc.

Anyway, Mr. Bos comes back to me with this bit: "_If_ you can show
that the program is unacceptably slow with Ian's version, and fast
enough with yours, then I'd still first look for a better algorithm;
and only if that cannot be found, I'd use your version, and add a big
comment explaining why I didn't use the obvious code."

As if to say: "Ugh! What a highly unclear, utterly vile little
expression you've come up with Mr. Henry. Prove to me it might be
faster on the OP's unstated architecture and toolchain, then I'll be
motivated to do you one better and if I can't and am forced to
actually use your putrid expression, I'll have to add a big comment
explaining my duress."

To me, that seemed an extreme reaction to and awfully tame expression.
I am pleased that you weren't so put off by it.

--
Dan Henry
Aug 21 '07 #22

P: n/a
Dan Henry said:
On Tue, 21 Aug 2007 00:24:53 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>>[Richard Bos] just didn't think it was as self-evident as the code
that preceded it in the thread.

For the sake of accuracy, the phrase he used was "It's highly
unclear..."
For the sake of even more accuracy, the phrase he used was "It's highly
unclear compared [to] Ian's", which is a rather different claim.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 21 '07 #23

P: n/a
Richard Heathfield wrote:
Dan Henry said:
>On Tue, 21 Aug 2007 00:24:53 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>>[Richard Bos] just didn't think it was as self-evident as the code
that preceded it in the thread.
For the sake of accuracy, the phrase he used was "It's highly
unclear..."

For the sake of even more accuracy, the phrase he used was "It's highly
unclear compared [to] Ian's", which is a rather different claim.
Goodness me, this thread is still going!

What are you doing working nights?

--
Ian Collins.
Aug 21 '07 #24

P: n/a
Ian Collins said:
What are you doing working nights?
Can't sleep. :-)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 21 '07 #25

P: n/a
pete wrote:
Richard Heathfield wrote:

pete said:

<snip>
I've indulged in witty sarcasm a lot on this newsgroup,
Well, half-witty, anyway.

And having failed to resist writing this reply,
I will now try very hard to resist posting it.

My posts that I have discarded before sending, oh boy, oh boy...
Sometimes it's helpful, like the old thing about writing exactly how
you feel in a letter to someone, then tearing it up rather than send it.

Brian
Aug 21 '07 #26

P: n/a
"Default User" <de***********@yahoo.comwrites:
pete wrote:
>Richard Heathfield wrote:
pete said:
<snip>

I've indulged in witty sarcasm a lot on this newsgroup,

Well, half-witty, anyway.

And having failed to resist writing this reply,
I will now try very hard to resist posting it.

My posts that I have discarded before sending, oh boy, oh boy...

Sometimes it's helpful, like the old thing about writing exactly how
you feel in a letter to someone, then tearing it up rather than send it.
The trick is to avoid sending it. Accidentally hitting the "send"
button (control-c control-c in my newsreader) is a lot easier than
accidentally putting a letter in an envelope, sealing it, putting a
stamp on it, and dropping it in a mailbox.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 21 '07 #27

P: n/a
Keith Thompson wrote:
"Default User" <de***********@yahoo.comwrites:
>pete wrote:
>>Richard Heathfield wrote:
pete said:
<snip>

I've indulged in witty sarcasm a lot on this newsgroup,

Well, half-witty, anyway.

And having failed to resist writing this reply,
I will now try very hard to resist posting it.

My posts that I have discarded before sending, oh boy, oh boy...

Sometimes it's helpful, like the old thing about writing exactly
how you feel in a letter to someone, then tearing it up rather
than send it.

The trick is to avoid sending it. Accidentally hitting the "send"
button (control-c control-c in my newsreader) is a lot easier than
accidentally putting a letter in an envelope, sealing it, putting
a stamp on it, and dropping it in a mailbox.
I have a 'draft' directory, and the reader can dump anything in
there in place of sending (or destruction). Very handy for cooling
off. Their usual fate is destruction.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 22 '07 #28

P: n/a
pete <pf*****@mindspring.comwrote:
Dan Henry wrote:

On Fri, 17 Aug 2007 10:38:52 +0200, Richard <rg****@gmail.comwrote:
>rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>
>Dan Henry <us****@danlhenry.comwrote:
>>
>>Amen
>>
>*Plonk*
>
>All the guy did, with an explanation, was to explain why he gave the
>alternate expression.
Alternate expressions seem to be allowed in c.l.c.
But like Mr. Heathfield said,
I think it was your witty sarcasm that got the plonk.
It was not the sarcasm. It was the hypocrisy.

Richard
Aug 23 '07 #29

This discussion thread is closed

Replies have been disabled for this discussion.