Hi Robert,
It can be done, but you have to work out which bits "fall off the end", and store them somewhere, by writing a flowchart to do it. Within the chip itself, bits which are right-shifted off the end ("underflows"), and bits which are left-shifted off the top ("overflows"), just disappear forever.
Here's an example of the kind of thing you need to do. It will work for your particular case where ULONG variables are used...
- Bitshifter Example.fcfx
- NB) Open the 'Component Debug' window to see the results (FC V6 required!)
- (5.76 KiB) Downloaded 315 times
Some explanation...
- The global variable 'Value' is the main value that you want to do the shifting on.
- The global variable 'Store' remembers the bits that underflow when they are right shifted off the end.
When right shifting, using the 'rightShift' macro...
1) The bits that would underflow from 'Value' are first isolated using a bitmask and placed in the '.underflow' local variable.
2) The .underflow variable is shifted left by enough places to push the bits over to the left hand end (target position in 'Store')
3) 'Value' and 'Store' are both shifted right as desired (bits from Value will underflow)
4) The '.underflow' bits are combined into 'Store' to memorise them.
When left shifting, using the 'leftShift' macro...
1) The bits from 'Store' that need recalling are isolated using a bitmask and place in the '.recover' local variable.
2) The .recover variable is shifted right by enough places to push the bits over to the right hand end (target position in 'Value')
3) 'Value' and 'Store' are left shifted as desired (bits from Store will overflow)
4) The '.recovered' bits are combined into 'Value' to return them to their original position.
LIMITATIONS
Note that these particular macros only work for a limited set of circumstances.
- If you begin with a left shift, you still lose any overflowed bits, because 'Store' is only working to remember 'underflowed' bits.
- Maximum right shift is 32 bits (length of a ULONG), and this is cumulative - a load of small shifts that add up to more than 32 still won't work.
- You can only recover a maximum of 32 bits - i.e. following a 4 bit right shift with 6 bits of left shift doesn't work.
In principle you could allow any amount of right and left shifts (up to the amount of available memory) by having long 'chains' of 'Store' variables both to the left and right of your 'Value' variable - each one passing its underflowed or overflowed bits to the next one along. It would be like having a 32-bit wide "window" looking into an endless row of bits.
These kind of techniques are exactly what is used in some programming languages that allow very large or very small numbers (you could think of 'Store' as being the 'Fractional' part of a non-integer number - a simple form of 'fixed point arithmetic'). This is why code libraries for things like floating point maths etc. take up so much memory - compared to that kind of thing, this example is "trivial", yet it still takes quite a lot of head-scratching to make it work!
All the best
Steve.