Shifting bits

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
User avatar
robertpatterson
Posts: 44
Joined: Thu Feb 21, 2013 2:16 am
Location: New England
Been thanked: 6 times

Shifting bits

Post by robertpatterson »

I had a number like 0x0fff shifted them 12 places to the right and it comes back as zeros when shifted back one at a time Is there a way to not lose the data
shifting back.

User avatar
SteveM
Posts: 55
Joined: Tue Mar 25, 2014 2:09 pm
Has thanked: 28 times
Been thanked: 65 times

Re: Shifting bits

Post by SteveM »

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.

User avatar
robertpatterson
Posts: 44
Joined: Thu Feb 21, 2013 2:16 am
Location: New England
Been thanked: 6 times

Re: Shifting bits

Post by robertpatterson »

Thanks Steve,

You guys are the best in the business. If flow code could do a shift left or shift right with out loosing the bits that would be a killer feature.

I don't know how I got myself into a bus data project that runs on nibbles I should have thought twice before I even started.

User avatar
robertpatterson
Posts: 44
Joined: Thu Feb 21, 2013 2:16 am
Location: New England
Been thanked: 6 times

Re: Shifting bits

Post by robertpatterson »

I did a work around by reversing all the bits on paper and sending them out like that.

Post Reply