How do I shift a number and get each bit 1 at a time
CelticLord
Posts: 50
Example: if I take a number say 148 and I want to shift it left 8 times but get each of the bits every time I shift it......
10010100 << 1 = 1
00101000 << 1 = 0
01010000 << 1 = 0
10100000 << 1 = 1
01000000 << 1 = 0
10000000 << 1 = 1
00000000 << 1 = 0
00000000 << 1 = 0
This is posible in asm with the flags but I want to do it in spin.
10010100 << 1 = 1
00101000 << 1 = 0
01010000 << 1 = 0
10100000 << 1 = 1
01000000 << 1 = 0
10000000 << 1 = 1
00000000 << 1 = 0
00000000 << 1 = 0
This is posible in asm with the flags but I want to do it in spin.
Comments
Bitwise and is the "&" symbol.
When I feed it
MaxOut (DOut, CLK, %10000010, 8)
I get the expected bits, but when I feed it...
MaxOut (DOut, CLK, %0110000000000000, 16)
I get all 0's
I think I was replying to a different post. The above code doesn't correspond to the question in post #3 (which I think CelticLord was still editing when I initially replied.)
I'll leave this post in case it's useful but it doesn't answer the question asked.
As i said it works fine on byte but all 0's with the word?
To keep it from reversing the bits.
You should be able to change the "8" to any number up to 32. If it won't shift more than 8 I suggest posting all your code as an archive. The problem may be elsewhere in the code.
All local variables are longs (32-bit).
I'm using strike out a lot today.
Sorry, the code I linked to was I2C code. I'll modify your code to do what I think you want it to. This will take a couple of minutes.
Edit: I changed my mind.
I'm working on your code to show you how to shift bits and I get this?
Good bye.
I would suggest shifting your data all the way to the left to align it with the most significant bit, which is the sign bit. That way you can determine the value of the bit by checking the sign of the value. To do this you should make Mout a long instead of a word.
Your initial alignment statement should then be:
Mout := ovalue << (32 - obits)
The first lines of your repeat loop would be
Tips:
-- define your connections to the MAX1487 as constants -- no need to pass pin #s (as you only have on device, right?)
-- learn the shift (<<, >>) and rotate (<-, ~>) operators as they are very powerful
Reviewing the data sheet, it looks like you can do 8- or 16-bit reads, and always with a control byte. I would code a general read routine like this:
Again, this assumes that the pins are defined as constants and setup (outputs where needed) in the initialization part of your program.
BTW, DIN is the Propeller pin connected to the MAX1497.DIN pin; DOUT is the Propeller pin connected to the MAX1497.DOUT pin.
Finally...
Comments like that are the fastest way to get you added to an "Ignore User" list (Congrats, you've already made Duane's). Nobody owes you anything here. Be polite and you'll get the help you need.