To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 24381
24380  |  24382
Subject: 
Re: Rotating through a four-bit nibble
Newsgroups: 
lugnet.robotics
Date: 
Wed, 21 Sep 2005 23:53:21 GMT
Viewed: 
1176 times
  
In lugnet.robotics, Kevin L. Clague wrote:
In lugnet.robotics, Brian Davis wrote:
   No, it's not pornographic (look over in rtlToronto - I hear it's a fun
thread). But I'm working in NQC, and I need to rotate the bit series in a 4-bit
(or 8-bit) nibble embedded in a 16-bit word. Annoying. Any suggestions? Masking
the desired four bits out of a storage variable and into a temporary variable
four times (so it's a repeating pattern) and then rotating and masking out the
desired sequence might work, but is there a better way? Bit-level manipulation
is not my strong suite.

(x's here are "something else" that I may wish to keep intact, y is garbage or
potentially trashed bits)

stored sequence in 16-bit word:        xxxx xxxx 0110 xxxx
4-bit nibble copied across temporary:  xxxx 0110 0110 0110
left shift temporary, say, twice:      xx01 1001 1001 10yy
mask out one nibble back into storage: xxxx xxxx 1001 xxxx

There's got to be a better way. I'm using NQC, so I'm stuck with those big
16-bit words and basic operations like bit logic, shifts, and interger division
(and of course modulo division).

   Anybody?

Since I didn't really complete the assignment I'll try again.  I didn't honor
the x bit request.

I confirmed that even SDK v2.5 does not have shifts or rotates, and that in NQC
you can only do shifts based on constant amounts.

The two routines below allouw rotation by a valiable amount.  If you know at
compile what the rotate amount is, then you can make this more efficient


void
rotate_right(   // this one requires two scratch variables.
  int value,
  int amount)
{
   int l,r;

   // truncate rotate amount to 3:0, anything higher
   // just maps back down to 3:0

   amount = amount & 3;   /* size -1 */

   switch (amount) {
     case 0:
       return;
     break;
     case 1:              // 3210 becomes 0321
       r  = value / 2;
       r &= 0x70;         // -321
       l  = value * 8;
       l &= 0x80;         // 0---
     break;
     case 2:              // 3210 becomes 1032
       r  = value / 4;
       r &= 0x30;         // --32
       l  = value * 4;
       l &= 0xc0;         // 10--
     break;
     case 3:              // 3210 becomes 2103
       r  = value / 8;
       r &= 0x10;         // ---3
       l  = value * 2;
       l &= 0xe0;         // 210-
     break;
   }
   value &= 0xff0f;   // zero out old value
   value |= r | l;    // merge in new value
}

void
rotate_right2(  // this one trashes amount, but only uses one scratch
  int value,
  int amount)
{
   int l;

   // truncate rotate amount to 3:0, anything higher
   // just maps back down to 3:0

   amount = amount & 3;   /* size -1 */

   switch (amount) {
     case 0:
       return;
     break;
     case 1:              // 3210 becomes 0321
       amount  = value / 2;
       amount &= 0x70;         // -321
       l  = value * 8;
       l &= 0x80;         // 0---
     break;
     case 2:              // 3210 becomes 1032
       amount  = value / 4;
       amount &= 0x30;         // --32
       l  = value * 4;
       l &= 0xc0;         // 10--
     break;
     case 3:              // 3210 becomes 2103
       amount  = value / 8;
       amount &= 0x10;         // ---3
       l  = value * 2;
       l &= 0xe0;         // 210-
     break;
   }
   value &= 0xff0f;   // zero out old value
   value |= amount | l;    // merge in new value
}

/*
* If you only want 2 bit rotate in the four bit field, try this
*/

void rotate2(int value)
{
  int l, r;

  r = (value / 4) & 0x30;
  l = (value * 4) & 0xc0;

  value &= 0xff0f;
  value |= r | l;
}

rotate2 compiles to 10 LASM byte codes.

You can see all the routine's LASM byte codes using

nqc -TRCX2 -L -c -s rotate.nqc

Kev



Message has 1 Reply:
  Re: Rotating through a four-bit nibble
 
(...) Both your 1st & 2nd solutions are very good; thanks to you and other posters to this thread for giving me a much better handle on binary manipulations, and solutions to this problem. In answer to an earlier question, speed is important (simple (...) (19 years ago, 22-Sep-05, to lugnet.robotics)

Message is in Reply To:
  Re: Rotating through a four-bit nibble
 
(...) Well, your example has the nibble of interest in the middle of the register. Due to boundary conditions, it might be cheaper to have it in the least significant bits of the 16 bit register. In your example you are rotating by an aribtrary (...) (19 years ago, 21-Sep-05, to lugnet.robotics)

13 Messages in This Thread:




Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR