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 / 24379
24378  |  24380
Subject: 
Re: Rotating through a four-bit nibble
Newsgroups: 
lugnet.robotics
Date: 
Wed, 21 Sep 2005 18:42:18 GMT
Viewed: 
1277 times
  
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?

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 amount.... so I'll try to code
to that.

Doing it one bit at time will be slow, so trying to do it in one pass is best.

LEGO firmware does not support the concept of shifting.  Originally NQC didn't
support the concept of shifting.  I pointed out to Dave Baum that he could use
multiply and divide to support shifting, so he added it to NQC.  It has been
many years, but I think shifting is only allowed with constant shift amounts.

I'll write the code explicitly using multiply and divide, because then we really
know what we are making the firmware do.

I will code based assuming that the nibble is in the four least significant
bits, but it could easily be changed to other bits in the register.

int
nibble_rotate_right(
  int value,   // The value to be rotated
  int amount)  // The rotate amount as a variable
{
  // truncate rotate amount to 3:0, anything higher just maps back down to 3:0

  amount &= 3;   // size -1

  switch (amount) {
    case 0:
      tmp = 0;           // rotate by zero leaves things unchanged
    break;
    case 1:
      tmp = value / 2;   // right shift by one
      value *= 8;        // left shift by 3
    break;
    case 2:
      tmp = value / 4;   // right shift by 2
      value *= 2;        // left shift by 2
    break;
    case 3:
      tmp = value / 8;   // right shift by 3
      value *= 2;        // left shift by 1
    break;
  }
  value |= tmp;      // merge
  value &= 0xf;      // clear all unused bits

  return value;
}

It may seem strange, but for four bits, I think this is the fastest, and
possibly the least amount of code.  Caveat Emptor:  I have not compiled this
code under NQC (I don't have NQC on my Sun workstation ;^)

Enjoy,
Kev



Message has 2 Replies:
  Re: Rotating through a four-bit nibble
 
(...) 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 (...) (19 years ago, 21-Sep-05, to lugnet.robotics)
  Re: Rotating through a four-bit nibble
 
(...) Why in the world wouldn't the firmware support shifting? The hardware (URL) certainly does>. Heh, using multiply and divide to simulate shifting is kind of backwards. (19 years ago, 22-Sep-05, to lugnet.robotics, FTX)

Message is in Reply To:
  Rotating through a four-bit nibble
 
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 (...) (19 years ago, 20-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