help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Right shifting in octave


From: Richardson, Anthony
Subject: RE: Right shifting in octave
Date: Sat, 3 May 2014 16:09:32 +0000

> From: address@hidden
> On Behalf Of goblinsly
> 
> Thank you for your answer,
> 
> I have to program crc in octave as a college asignment. The problem is that
> i am a total beginner as far as octave goes. That is why i am using the
> problem we were given in c as a guideline. It is proving to be useless
> though since, as you pointed, octave and c handle number differently.
> 
> If i write number 0xFFFFFFFF in C ( unsigned int ), the result is -1, if i
> write it in octave i get a very big number ( 4294967295). How would i force
> octave to get -1 ?
> 

As others have pointed out Octave is not the best language for solving your 
problem.
It does have support for other data types, you just have to be very careful 
using the
various casting and typecasting functions.  Here is how you could do the 
equivalent
to the above:

        a = typecast(hex2num('ffffffff','single'),'int32')

Here a will be of type int32 and will be equal to -1.

Octave code that will give the same results as the C code in your original post 
is:

        crc = typecast(int32(-85),'uint32');
        a = bitshift(crc,-1);

Here crc and a will both be of type uint32.  crc will be equal to 4294967211
and a will be equal to 2147483605.  If you really are intent on doing this you
will need to check the data type of the return from each function.   The data 
type
seems to be preserved for basic operations (+,-,*,/), but most functions will
return arguments of type double.  Many functions will not accept arguments
of anything other than type double or single.

Tony Richardson






reply via email to

[Prev in Thread] Current Thread [Next in Thread]