help-octave
[Top][All Lists]
Advanced

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

Re: Right shifting in octave


From: Kai Torben Ohlhus
Subject: Re: Right shifting in octave
Date: Fri, 2 May 2014 11:10:55 +0200

On Fri, May 2, 2014 at 3:35 AM, goblinsly <address@hidden> wrote:
Hey everyone,

I am writting a simple crc32 program in octave, i have a working program
written in c as a guideline/help. Right now i am stuck with the
rightshifting:

in c:

unsigned int crc = -85
unsigned int number = (crc >> 1);

The result is a really big number 2147483605. Basicly i get the same result
as if i put 0xFFFFFFFF instead of -85. And the program works.

In Octave i get a really small number -43:
crc  = -85
a = bitshift(crc, -1)

How can i duplicate the big result from c in octave ?



--
View this message in context: http://octave.1599824.n4.nabble.com/Right-shifting-in-octave-tp4663871.html
Sent from the Octave - General mailing list archive at Nabble.com.

_______________________________________________
Help-octave mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/help-octave

In general it is a bad idea to translate C-Programs to Octave in a naive way. The problem you are faced with are different implementations of the integer types. In C you can make full use of the two's complement [1], means a right shift of a negative number will always create a non-negative number of magnitude >= 2^30. Even your initialization of crc in your C program makes use of "dirty" integer arithmetic (how can an unsigned int ever be -85 ?). Integer arithmetic in Octave is treated differently [2] and most of the computation is mixed with the double data-type.

To fix your program (again, bad idea if you don't know what you do!) use:

crc = -85;
if (crc < 0)
  crc = intmax ('uint32') + crc;
end
a = bitshift (crc, -1)

As I don't know a CRC32 function for Octave and your C program does the required job, consider to use your C-Code via an OCT-File [3].

HTH,
Kai


reply via email to

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