most significant bit extraction

  • Thread starter Thread starter platinum_eye200
  • Start date Start date
P

platinum_eye200

is there a function to extract the two most significant bits from an
bit binary number, so that I may put them on the end of another.

eg take 11 from 11000000 and 'add' to 00000000 to form 0000000011

thanks,

marti
 
Not sure, but here is a different idea. By shifting two places, you are
multiplying by 4. You then are adding either a 2 (1,0), or a 3 (1,1). This
idea may work for larger numbers since the built-in HEX2BIN is rather
limited.

=A1*4+IF(MOD(ROUND(LOG(A1)/LOG(2),10),1)>=0.5,3,2)

Hope I got this right. :>)

HTH
Dana DeLouis
 
Not sure, but here is a different idea. By shifting two places, you are
multiplying by 4. You then are adding either a 2 (1,0), or a 3 (1,1). This
idea may work for larger numbers since the built-in HEX2BIN is rather
limited.

=A1*4+IF(MOD(ROUND(LOG(A1)/LOG(2),10),1)>=0.5,3,2)
...

First off, to match up with the OP's cell addresses, this should be

=F29*4+IF(MOD(ROUND(LOG(HEX2DEC(J29),2),10),1)>=0.5,3,2)

Now, I'll agree that the OP's formula

=HEX2BIN(F29,8)&LEFT(HEX2BIN(J29),2)

is equivalent to left shifting twice and adding 2 or 3 *MOST* of the time,
but not *ALL* of the time. If J29 were either 0 or 1, then HEX2BIN(J29)
returns either "0" or "1", respectively, and LEFT("0",2) = "0" while
LEFT("1",2) returns "1". So your formula doesn't match up with the OP's
formula when J29 is 0 or 1. Indeed, if J29 were zero, your formula returns
#NUM!. Even for positive J29, your formula can return incorrect results.

J29 == "17" (hex):
MOD(ROUND(LOG(HEX2DEC(J29),2),10),1) == 0.523561956 >= 0.5, so 3 or "11"
(bin),
but 23 (dec) == 10111 (bin), so the top bits are "10".

Similarly for 46-47, 91-95 and 182-191 on 0-255. You should have used

=F29*4+IF(MOD(ROUND(2^LOG(HEX2DEC(J29),2),10),1)>=0.5,3,2)

That said, it's almost certain the OP's formula is wrong. I'd bet the OP
meant
to use either

=HEX2BIN(F29,8)&LEFT(HEX2BIN(J29,2),2)

or

=HEX2BIN(F29,8)&LEFT(HEX2BIN(J29,8),2)

If not, and the OP's formula really is correct for the OP's needs, then the
OP's formula is optimal, and bit shifting unnecessary (and awkward).
 
but 23 (dec) == 10111 (bin), so the top bits are "10".

Thanks Harlan. I totally missed that there are a lot of numbers like you
mentioned that won't work. Thanks for the catch. I was trying to come up
with something clever for larger numbers. My guess now is that the OP's
formula is optimal. I'm basing it on the subject "most significant bit." By
the op's use of HEX2BIN(J29) without the second option, this insures the
first bit returned will be a "1." (assuming >0). Anyway, thanks again.

Dana DeLouis
 

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

Back
Top