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:
how to do logical bit shift?

Subject: how to do logical bit shift?

From: yajnesh padiyar

Date: 28 Sep, 2009 15:12:02

Message: 1 of 12

I want to use logical bit shift ...... I used mathworks link to find the way http://www.mathworks.com/access/helpdesk/help/toolbox/fixedpoint/index.html?/access/helpdesk/help/toolbox/fixedpoint/ref/bitsrl.html&http://www.google.co.in/search?hl=en&source=hp&q=logical+bit+shift+matlab&btnG=Google+Search&meta=&aq=f&oq=

but as said in the page it says use bitsll and bitsrl

but I tried in matlab it says function not found...


can any one help me out of this

Subject: how to do logical bit shift?

From: Matt Fig

Date: 28 Sep, 2009 17:00:21

Message: 2 of 12

Well, as the documentation indicates: Do you have the fixed point toolbox?

Subject: how to do logical bit shift?

From: yajnesh padiyar

Date: 29 Sep, 2009 14:50:19

Message: 3 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <h9qq35$b93$1@fred.mathworks.com>...
> Well, as the documentation indicates: Do you have the fixed point toolbox?



Thank you for your kind reply

I do not know have fixed point tool bar.
Do i have any alternative to perform logical shift operation

Subject: how to do logical bit shift?

From: Thomas Clark

Date: 29 Sep, 2009 15:18:03

Message: 4 of 12

It kind of depends on what you want to do. You've unfortunately not described what you're doing very well.

To me, 'logical bit shift' means:

a = logical([0 1 1 0 0 0 1])
shifted_a = [0 a(1:end-1)}
% Which gives shifted_a = [0 0 1 1 0 0 0 1]

The logical data type in Matlab does not take up one bit per element - it takes up 8 bits (1 byte) per element, for historic reasons (it's also quicker to use for most arrays due to the computational architecture).

So bit shifting a logical isn't a true bitshift.

If you want a true bitshift, define your data as an integer values and use matlab's built in bitshift() function.

You can convert between integer and binary types using quick and easy sets of commands. Consider, for example, this offering on the file exchange:
http://www.mathworks.com/matlabcentral/fileexchange/19744

Hope this helps.

Tom Clark


"yajnesh padiyar" <yp171988@gmail.com> wrote in message <h9t6rb$bq8$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <h9qq35$b93$1@fred.mathworks.com>...
> > Well, as the documentation indicates: Do you have the fixed point toolbox?
>
>
>
> Thank you for your kind reply
>
> I do not know have fixed point tool bar.
> Do i have any alternative to perform logical shift operation

Subject: how to do logical bit shift?

From: yajnesh padiyar

Date: 30 Sep, 2009 17:18:03

Message: 5 of 12

thank you again for your reply

i want to do logical shift of a 32 bit binary number 4 bit to left and 5 bit to right......

as u hav shown it will be easy for small length numbers but for 32 bit I felt it diffficult......

can i know what solution do i have now?

"Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <h9t8fb$t8h$1@fred.mathworks.com>...
> It kind of depends on what you want to do. You've unfortunately not described what you're doing very well.
>
> To me, 'logical bit shift' means:
>
> a = logical([0 1 1 0 0 0 1])
> shifted_a = [0 a(1:end-1)}
> % Which gives shifted_a = [0 0 1 1 0 0 0 1]
>
> The logical data type in Matlab does not take up one bit per element - it takes up 8 bits (1 byte) per element, for historic reasons (it's also quicker to use for most arrays due to the computational architecture).
>
> So bit shifting a logical isn't a true bitshift.
>
> If you want a true bitshift, define your data as an integer values and use matlab's built in bitshift() function.
>
> You can convert between integer and binary types using quick and easy sets of commands. Consider, for example, this offering on the file exchange:
> http://www.mathworks.com/matlabcentral/fileexchange/19744
>
> Hope this helps.
>
> Tom Clark
>
>
> "yajnesh padiyar" <yp171988@gmail.com> wrote in message <h9t6rb$bq8$1@fred.mathworks.com>...
> > "Matt Fig" <spamanon@yahoo.com> wrote in message <h9qq35$b93$1@fred.mathworks.com>...
> > > Well, as the documentation indicates: Do you have the fixed point toolbox?
> >
> >
> >
> > Thank you for your kind reply
> >
> > I do not know have fixed point tool bar.
> > Do i have any alternative to perform logical shift operation

Subject: how to do logical bit shift?

From: arich82

Date: 30 Sep, 2009 18:20:19

Message: 6 of 12

> i want to do logical shift of a 32 bit binary number 4 bit to left and 5 bit to right......

Why can't you just multiply your integer by 16 then divide by 32?

I'm not sure what you want exactly, but see if any of this helps:

A = 2^32-1; % defaults as DOUBLE
A = uint32(A); % converts A to UINT32
B = bitshift(A, 4);
C = bitshift(B, -5);

dec2bin(A, 32) % just displays A as binary string; doesn't affect anything
dec2bin(B, 32)
dec2bin(C, 32)

Subject: how to do logical bit shift?

From: yajnesh padiyar

Date: 1 Oct, 2009 18:14:02

Message: 7 of 12

i tried many ways to shift the 32 bit number

the bitshift function and the process of multiplication are leading to two different results

A=uint32(2654435769);

B=bitshift(A,4);

C=A*(2^4);

result

A=2654435769

B=3816266640

C=4294967295

I really wanna know what is happening
"arich82 " <|a|r|i|c|8|2|@hotmail.com> wrote in message <ha07h3$pj9$1@fred.mathworks.com>...
> > i want to do logical shift of a 32 bit binary number 4 bit to left and 5 bit to right......
>
> Why can't you just multiply your integer by 16 then divide by 32?
>
> I'm not sure what you want exactly, but see if any of this helps:
>
> A = 2^32-1; % defaults as DOUBLE
> A = uint32(A); % converts A to UINT32
> B = bitshift(A, 4);
> C = bitshift(B, -5);
>
> dec2bin(A, 32) % just displays A as binary string; doesn't affect anything
> dec2bin(B, 32)
> dec2bin(C, 32)

Subject: how to do logical bit shift?

From: Ashish Uthama

Date: 1 Oct, 2009 18:21:03

Message: 8 of 12

On Thu, 01 Oct 2009 14:14:02 -0400, yajnesh padiyar <yp171988@gmail.com>
wrote:

> i tried many ways to shift the 32 bit number
>
> the bitshift function and the process of multiplication are leading to
> two different results
>
> A=uint32(2654435769);
>
> B=bitshift(A,4);
>
> C=A*(2^4);
>
> result
>
> A=2654435769
>
> B=3816266640
>
> C=4294967295
>
> I really wanna know what is happening
> "arich82 " <|a|r|i|c|8|2|@hotmail.com> wrote in message
> <ha07h3$pj9$1@fred.mathworks.com>...
>> > i want to do logical shift of a 32 bit binary number 4 bit to left
>> and 5 bit to right......
>>
>> Why can't you just multiply your integer by 16 then divide by 32?
>>
>> I'm not sure what you want exactly, but see if any of this helps:
>>
>> A = 2^32-1; % defaults as DOUBLE
>> A = uint32(A); % converts A to UINT32
>> B = bitshift(A, 4);
>> C = bitshift(B, -5);
>>
>> dec2bin(A, 32) % just displays A as binary string; doesn't affect
>> anything
>> dec2bin(B, 32)
>> dec2bin(C, 32)

You are reaching the limit with that multiplication.
intmax('uint32') (==C)

Subject: how to do logical bit shift?

From: arich82

Date: 1 Oct, 2009 20:57:02

Message: 9 of 12

"Ashish Uthama" <first.last@mathworks.com> wrote in message <op.u04vxd2ca5ziv5@uthamaa.dhcp.mathworks.com>...
> On Thu, 01 Oct 2009 14:14:02 -0400, yajnesh padiyar <yp171988@gmail.com>
> wrote:
>
> > i tried many ways to shift the 32 bit number
> >
> > the bitshift function and the process of multiplication are leading to
> > two different results
> >
> > A=uint32(2654435769);
> >
> > B=bitshift(A,4);
> >
> > C=A*(2^4);
> >
> > result
> >
> > A=2654435769
> >
> > B=3816266640
> >
> > C=4294967295

Look at dec2bin(C, 32) and you'll see why (like the poster before said, you're saturating your 32bit UINT).

Does the bitshift give you the result you're looking for? Do you have a known example that you've done by hand to compare it to? I'm still not clear on what you're trying to achieve if you're not satisfied with the bitshift result . . .


--

Subject: how to do logical bit shift?

From: yajnesh padiyar

Date: 2 Oct, 2009 16:17:03

Message: 10 of 12

A=uint32(((5^.5)-1)*(2^31));

B=bitshift(A);

the above operation gives me the required result but i have some constraints in my design

A=uint32(((5^.5)-1)*(2^31));
%A=2654435769
AH=dec2hex(A);
%AH=9E3779B9

%now I want a matrix 1X4
%U=[9E,37,79,B9];

and then I use U for some process and then again I want the processes U back as AH

then I use AH for bitshift

I have tried my level best to explain the problem

please help me




"arich82 " <|a|r|i|c|8|2|@hotmail.com> wrote in message <ha352u$1je$1@fred.mathworks.com>...
> "Ashish Uthama" <first.last@mathworks.com> wrote in message <op.u04vxd2ca5ziv5@uthamaa.dhcp.mathworks.com>...
> > On Thu, 01 Oct 2009 14:14:02 -0400, yajnesh padiyar <yp171988@gmail.com>
> > wrote:
> >
> > > i tried many ways to shift the 32 bit number
> > >
> > > the bitshift function and the process of multiplication are leading to
> > > two different results
> > >
> > > A=uint32(2654435769);
> > >
> > > B=bitshift(A,4);
> > >
> > > C=A*(2^4);
> > >
> > > result
> > >
> > > A=2654435769
> > >
> > > B=3816266640
> > >
> > > C=4294967295
>
> Look at dec2bin(C, 32) and you'll see why (like the poster before said, you're saturating your 32bit UINT).
>
> Does the bitshift give you the result you're looking for? Do you have a known example that you've done by hand to compare it to? I'm still not clear on what you're trying to achieve if you're not satisfied with the bitshift result . . .
>
>
> --

Subject: how to do logical bit shift?

From: Steven Lord

Date: 2 Oct, 2009 16:25:25

Message: 11 of 12


"yajnesh padiyar" <yp171988@gmail.com> wrote in message
news:ha591v$5v6$1@fred.mathworks.com...
> A=uint32(((5^.5)-1)*(2^31));
>
> B=bitshift(A);
>
> the above operation gives me the required result but i have some
> constraints in my design
>
> A=uint32(((5^.5)-1)*(2^31));
> %A=2654435769
> AH=dec2hex(A);
> %AH=9E3779B9

Note that this gives you a 1-by-8 char array.

> %now I want a matrix 1X4
> %U=[9E,37,79,B9];

You can't, not with the AH you described above (each character is an
individual element.) You could get a 1-by-4 matrix whose values are the hex
values 9E, 37, 79, and B9:

format hex
A = uint32(((5^.5)-1)*(2^31))
U = fliplr(typecast(A, 'uint8'))

Note that the elements of U in this procedure are NOT character arrays but
are uint8 arrays. If you wanted them as strings, I'd use SPRINTF with the
%x format specifier.

> and then I use U for some process and then again I want the processes U
> back as AH

TYPECAST the array back.

You may also find the SWAPBYTES function useful as you perform this bit
manipulation.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: how to do logical bit shift?

From: yajnesh padiyar

Date: 3 Oct, 2009 17:07:01

Message: 12 of 12

thank you all

I am very glad to tell that my problem is solved and my design is working fine


"Steven Lord" <slord@mathworks.com> wrote in message <ha59gq$6kv$1@fred.mathworks.com>...
>
> "yajnesh padiyar" <yp171988@gmail.com> wrote in message
> news:ha591v$5v6$1@fred.mathworks.com...
> > A=uint32(((5^.5)-1)*(2^31));
> >
> > B=bitshift(A);
> >
> > the above operation gives me the required result but i have some
> > constraints in my design
> >
> > A=uint32(((5^.5)-1)*(2^31));
> > %A=2654435769
> > AH=dec2hex(A);
> > %AH=9E3779B9
>
> Note that this gives you a 1-by-8 char array.
>
> > %now I want a matrix 1X4
> > %U=[9E,37,79,B9];
>
> You can't, not with the AH you described above (each character is an
> individual element.) You could get a 1-by-4 matrix whose values are the hex
> values 9E, 37, 79, and B9:
>
> format hex
> A = uint32(((5^.5)-1)*(2^31))
> U = fliplr(typecast(A, 'uint8'))
>
> Note that the elements of U in this procedure are NOT character arrays but
> are uint8 arrays. If you wanted them as strings, I'd use SPRINTF with the
> %x format specifier.
>
> > and then I use U for some process and then again I want the processes U
> > back as AH
>
> TYPECAST the array back.
>
> You may also find the SWAPBYTES function useful as you perform this bit
> manipulation.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Tags for 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