Ripping a single bit out of a Long
Sariel
Posts: 182
I am trying to take just the MSB of a long (one bit of info), and depending on if it is a 1 or a 0 do some other program trickery with if/else statements. Is there an easy way of doing this? I initially tried this:
(L[BasePin] is a 128 long buffer that is loaded from an SD card earlier in the program, just to get that clarified.)
but that really gets confusing since that is actually a negative number when converted to decimal, so there is no way to base a greater than/less than off of this method when taking into consideration that the rest of the bits in the var "NetTemp" could be anything, making it a positive or negative number. I also tried:
and I was still getting odd results. anyone know of some way of isolating just the MSB, even if it is to another variable so that I can do some true/false work?
NetTemp := L[BasePin] repeat 32 if NetTemp => 00_0000_0000_0000_0000_0000_0000_0000 ..... else ... NetTemp <-= 1
(L[BasePin] is a 128 long buffer that is loaded from an SD card earlier in the program, just to get that clarified.)
but that really gets confusing since that is actually a negative number when converted to decimal, so there is no way to base a greater than/less than off of this method when taking into consideration that the rest of the bits in the var "NetTemp" could be anything, making it a positive or negative number. I also tried:
NetTemp := ||L[BasePin] if NetTemp => 00_0000_0000_0000_0000_0000_0000_0000 .....
and I was still getting odd results. anyone know of some way of isolating just the MSB, even if it is to another variable so that I can do some true/false work?
Comments
if x < 0
Jonathan
I assume that does not matter, and lonesock is still correct?
Duane J
I do
Where...
value = 1 or 0
in = the original number
bit = what bit in the long.
Thanks !
Clever code :-)
This is my version from a user-friendly programming template for the EFX-TEK HC-8+
My version is a little less clever than the neat one-liner that Bits showed you; again, mine is built into a programming template that is intended to be easy on Spin newbies. Mine will also set the bit for any non-zero value in the newbit parameter. This allows me to use true and false results from other operations, for example, as in this program I was working on yesterday that looks for specific numbered files on an SD card
Your solution is pretty close to my first thoughts, it's very easily understood and has indeed an advantage with non-zero-or-one values.
Bits solution seems efficient (faster ?), so I guess it's still true that there are pros and cons with every design.
/Nick