Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Binary rotation with blockage

Subject: Binary rotation with blockage

From: Mark

Date: 22 Mar, 2009 13:57:01

Message: 1 of 4

Hi,

I want to rotate a binary number by one each iteration but only if the adjacent location is empty.

Examples:

01110000 becomes
00111000

01011011 becomes
00101111

01110011 becomes
00111011

I could use a loop to perform the function but thought there might be a math function to collapse the array in this fashion.

Thanks for looking.

Subject: Binary rotation with blockage

From: Roger Stafford

Date: 22 Mar, 2009 21:59:02

Message: 2 of 4

"Mark" <medwar19.nospam@hotmail.com> wrote in message <gq5g3d$kch$1@fred.mathworks.com>...
> Hi,
>
> I want to rotate a binary number by one each iteration but only if the adjacent location is empty.
>
> Examples:
>
> 01110000 becomes
> 00111000
>
> 01011011 becomes
> 00101111
>
> 01110011 becomes
> 00111011
>
> I could use a loop to perform the function but thought there might be a math function to collapse the array in this fashion.
>
> Thanks for looking.

  You haven't specified the form your binary numbers are to be in, Mark. They might be in a binary string, a vector of 1's and 0's, a single double number, a single fixed-point number, or whatever.

  I'll assume here that they are in the form of a row vector of 1 and 0 digit values (not a string) with the most significant binary digits first. Call such a vector x and do this:

 n = max([find(diff([x 1])>0),1]); %Locate the rightmost 0 digit
 x(1:n) = [x(n),x(1:n-1)]; % Rotate a portion of x to the right by one

Then x is rotated by one as you require.

  If your number is in a different form, you can probably convert the code above fairly easily. The essential idea is to locate the rightmost zero binary digit and rotate right by one all the digits from there on to the left of it. The purpose of the 'max' call is to avoid referencing x(0) when there is nothing to rotate.

Roger Stafford

Subject: Binary rotation with blockage

From: Roger Stafford

Date: 23 Mar, 2009 04:40:03

Message: 3 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gq6cb6$40h$1@fred.mathworks.com>...
> ......
> n = max([find(diff([x 1])>0),1]); %Locate the rightmost 0 digit
> x(1:n) = [x(n),x(1:n-1)]; % Rotate a portion of x to the right by one
> ......

  I didn't need to do a 'diff' operation in that code I sent. The following would have been sufficient.

 f = find([true,x<1]);
 n = max(f(end)-1,1);
 x(1:n) = [x(n),x(1:n-1)];

Roger Stafford

Subject: Binary rotation with blockage

From: Mark

Date: 23 Mar, 2009 18:05:17

Message: 4 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gq73r3$el1$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gq6cb6$40h$1@fred.mathworks.com>...
> > ......
> > n = max([find(diff([x 1])>0),1]); %Locate the rightmost 0 digit
> > x(1:n) = [x(n),x(1:n-1)]; % Rotate a portion of x to the right by one
> > ......
>
> I didn't need to do a 'diff' operation in that code I sent. The following would have been sufficient.
>
> f = find([true,x<1]);
> n = max(f(end)-1,1);
> x(1:n) = [x(n),x(1:n-1)];
>
> Roger Stafford

Hi Roger,

Thanks for the reply!
The operation is on a large boolean array and your snippit works a treat.
I new that a simple solution existed, I just wish I was skilled enough to find it myself!

Thanks again,

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us