# FPGA Central

## World's 1st FPGA Portal

 FPGA Groups shift operator

 Verilog comp.lang.verilog newsgroup / usenet

07-05-2006, 08:08 AM
 raghu Guest Posts: n/a
shift operator

hi all,
can anyone please tell me how to do a left shift in the folowing
manner.
suppose there are 5bits: 10010 now i have to shift left such that 1st
bit should go to the last position i.e 00101. when I use "<<" operator
the last position is appended with 0 i.e 00100. Is there any operator
in verilog to use?

Thanks a lot.

Regards,
Raghu

07-05-2006, 08:28 AM
 Jonathan Bromley Guest Posts: n/a
Re: shift operator

On 5 Jul 2006 00:08:24 -0700, "raghu" wrote:

>can anyone please tell me how to do a left shift in the folowing
>manner.
>suppose there are 5bits: 10010 now i have to shift left such that 1st
>bit should go to the last position i.e 00101. when I use "<<" operator
>the last position is appended with 0 i.e 00100. Is there any operator

This isn't a left shift; it's a rotate.

If the rotate is by a fixed number of bit positions, it's easy using
concatenation:

reg [4:0] R;
....
R = { R[3:0], R[4] }; // rotate left by 1 position
R = { R[2:0], R[4:3] }; // rotate left by 2 positions

If the shift is by a variable number of bit positions, it's a little
trickier. You could write a function that used a for-loop to
copy the bits; or you could use the "funnel shift" trick where
you do a shift on the concatenation of two copies of the thing:

reg [4:0] R;
reg [4:0] junk;
integer rotate_count;
....
{ R, junk } = { R, R } << rotate_count;

Hope this helps
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
[email protected]
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
07-10-2006, 08:17 PM
 Guest Posts: n/a
Re: shift operator

raghu wrote:
> hi all,
> can anyone please tell me how to do a left shift in the folowing
> manner.
> suppose there are 5bits: 10010 now i have to shift left such that 1st
> bit should go to the last position i.e 00101. when I use "<<" operator
> the last position is appended with 0 i.e 00100. Is there any operator
> in verilog to use?

Another alternative way of doing a rotate is by using a separate shift
to get the upper bits down to the correct position and then OR them
into the result:

rotatedN = (value << N) | (value >> (width-N));

07-11-2006, 09:50 AM
 Michael Guest Posts: n/a
Re: shift operator

I like this way much better!
[email protected] wrote:
> raghu wrote:
> > hi all,
> > can anyone please tell me how to do a left shift in the folowing
> > manner.
> > suppose there are 5bits: 10010 now i have to shift left such that 1st
> > bit should go to the last position i.e 00101. when I use "<<" operator
> > the last position is appended with 0 i.e 00100. Is there any operator
> > in verilog to use?

>
> Another alternative way of doing a rotate is by using a separate shift
> to get the upper bits down to the correct position and then OR them
> into the result:
>
> rotatedN = (value << N) | (value >> (width-N));

07-11-2006, 10:15 AM
 Jonathan Bromley Guest Posts: n/a
Re: shift operator

On 11 Jul 2006 01:50:39 -0700, Michael <[email protected]> wrote:

[Steven Sharp]
>> Another alternative way of doing a rotate is by using a separate shift
>> to get the upper bits down to the correct position and then OR them
>> into the result:
>>
>> rotatedN = (value << N) | (value >> (width-N));

[Michael]
>I like this way much better!

Yes, but it might be worth noting that concatenation of two
copies of the source value is completely trivial in synthesis,
whereas calculating the shift value (width-N) might not be.

I haven't tried this out thoroughly in synthesis yet. I've no
doubt that some tools will correctly infer a rotate operation
from Steven Sharp's rotate expression, but I rather fear
that others won't.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
[email protected]
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
07-11-2006, 04:52 PM
 Guest Posts: n/a
Re: shift operator

Jonathan Bromley wrote:
>
> Yes, but it might be worth noting that concatenation of two
> copies of the source value is completely trivial in synthesis,
> whereas calculating the shift value (width-N) might not be.

You have a good point about synthesis. I wouldn't expect a
problem with the subtract in the shift value, since it is just a
re-numbering of the decode on the shift mux, but I don't have
much experience with synthesis. There may be other potential
inefficiencies in synthesis if it does not recognize this idiom.
It might produce two shift muxes and OR the results together,
rather than recognizing that the bits affected by each shift are
mutually exclusive and a single shift mux with appropriate
input wiring can do the job. I don't know whether lower-level
logic optimization would recognize this at the boolean level.

 Bookmarks

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post [email protected] Verilog 3 05-26-2006 03:47 AM Russell Fredrickson Verilog 9 01-19-2005 07:33 PM Sridhar_Gadda Verilog 2 10-10-2004 04:22 PM Sachin Chandra Verilog 1 01-11-2004 07:24 PM Denis Gleeson Verilog 5 11-08-2003 06:59 PM

All times are GMT +1. The time now is 01:50 AM.