435,037 Members | 2,065 Online
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
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 0100and a known portion of an unknown byte, "byte 2":bbbb 0100I 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 wrote: >Ian Collins wrote: >philbo30 wrote: >>Simple question, I think:I have a known byte, "byte 1":0000 0100and a known portion of an unknown byte, "byte 2":bbbb 0100I 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 0100and a known portion of an unknown byte, "byte 2":bbbb 0100I 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. -- Posted via a free Usenet account from http://www.teranews.com Aug 11 '07 #7

 P: n/a Dan Henry 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 Ian Collins >philbo30 wrote:I need to compare the lower 4 bits (0100) of each and determineif "byte 1, lower 4" == "byte 2, lower 4". >>Oops, (a&0x0f) == (b&0x0f) !((a ^ b) & 0xF)Will also work. Efficiency is architecture dependent, so testfor 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. -- 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 On Sun, 12 Aug 2007 11:13:04 +1200, Ian Collins 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 tobe 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 showthat the program is unacceptably slow with Ian's version, and fastenough with yours, then I'd still first look for a better algorithm; andonly if that cannot be found, I'd use your version, and add a bigcomment 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 On Sun, 12 Aug 2007 11:13:04 +1200, Ian Collins 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 tobe 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 showthat the program is unacceptably slow with Ian's version, and fastenough with yours, then I'd still first look for a better algorithm; andonly if that cannot be found, I'd use your version, and add a bigcomment 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

 P: n/a On Wed, 15 Aug 2007 12:57:04 GMT, rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote: >Dan Henry On Mon, 13 Aug 2007 06:41:33 GMT, rl*@hoekstra-uitgeverij.nl (RichardBos) wrote:I don't know the OP's architecture or toolchain. Was that tinyexpression obfuscated to the point of being incomprehensible at acasual glance? I'd guess that you have run across the '&' operatorbefore, so was it the '^' (exclusive-or) operator that threw you? Let's put it this way. Your line was immediately obvious at a moment'sthought. Ian's line was immediately obvious _without_ having to thinkabout 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

 P: n/a rl*@hoekstra-uitgeverij.nl (Richard Bos) writes: Dan Henry 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 rl*@hoekstra-uitgeverij.nl (Richard Bos) writes: >Dan Henry >Amen *Plonk* All the guy did, with an explanation, was to explain why he gave thealternate 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 >rl*@hoekstra-uitgeverij.nl (Richard Bos) writes: >>Dan Henry -- Richard Heathfield Email: -www. +rjh@ Google users: "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

 P: n/a pete said: 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: 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 Dan Henry said: >It could have beenthat 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 Iprefer foo == 0 to !foo). Nor did Richard Bos - you can be sure ofthat. He rightly enjoys a great deal of respect in this newsgroup forhis C knowledge, and would have had no trouble working out what yourcode means. He just didn't think it was as self-evident as the codethat 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 wrote: >Dan Henry wrote: >>On Fri, 17 Aug 2007 10:38:52 +0200, Richard rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:Dan Henry

 P: n/a Dan Henry said: On Tue, 21 Aug 2007 00:24:53 +0000, Richard Heathfield >[Richard Bos] just didn't think it was as self-evident as the codethat 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. -- Richard Heathfield Email: -www. +rjh@ Google users: "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>[Richard Bos] just didn't think it was as self-evident as the codethat preceded it in the thread. For the sake of accuracy, the phrase he used was "It's highlyunclear..." 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 Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Aug 21 '07 #25

 P: n/a pete wrote: Richard Heathfield wrote: pete said: 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" Richard Heathfield wrote: pete said: 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 San Diego Supercomputer Center <* "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" pete wrote: >>Richard Heathfield wrote:pete said: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 exactlyhow you feel in a letter to someone, then tearing it up ratherthan 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. -- Posted via a free Usenet account from http://www.teranews.com Aug 22 '07 #28

 P: n/a pete rl*@hoekstra-uitgeverij.nl (Richard Bos) writes: > >Dan Henry > >>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