Mostrando entradas con la etiqueta programacion. Mostrar todas las entradas
Mostrando entradas con la etiqueta programacion. Mostrar todas las entradas

sábado, 9 de julio de 2016

El maximo valor de enteros en java y el 0x3f3f3f3f

stackoverflow.com/questions/5771520/why-is-the-maximum-value-of-an-unsigned-n-bit-integer-2n-1-and-not-2n

porque teniendo un valor entero de 32 bits su maximo valor es 2^31-1 y no 2^31-1 
Respuesta

@HardikThaker - Yes, it does. (Actually, in Java, Integer.MAX_VALUE--for 32 bit integers--is 2^31 - 1 andLong.MAX_VALUE--for 64 bit integers--is 2^63 - 1 because the sign bit is reserved.) – Ted Hopp Sep 29 '13 at 0:03 


ADEMAS que es el  0x3f3f3f3f

0x3f3f3f3f; // Numero muy grande para que no afecte al minHeap.

 //memset(m,0x3f,sizeof(m));

0x3f3f3f3f + 0x3f3f3f3f doesn't overflow int32

http://prog3.com/sbdm/blog/u013163567/article/details/19837805
http://ideone.com/cqFDL5
http://stackoverflow.com/questions/18429021/why-is-infinity-0x3f3f3f3f

viernes, 27 de mayo de 2016

Operador modulo con numeros negativos


The Quickest and easiest way to find the mod of a negative number is by using the below property
if a = (b) mod c then a = (c*k + b) mod c (where k = 1,2,3.......) It simply says that the value of a is unchanged when we add a multiple of c to b
Example
a = (10) mod 3 we all know that a = 1 Now
a = (3*1 + 10) mod 3 - a is still = 1
a = (3*2 + 10) mod 3 - a is still = 1
a = (3*3 + 10) mod 3 - a is still = 1
a = (3*4 + 10) mod 3 - a is still = 1
So adding any multiple of 3 (> 0) to 10 does not effect the value of a Now we use this to our advantage in finding mod of negative numbers
Example
a = (-10) mod 3 Now i add 12 to 10 as 12 is a multiple of 3 and hence the value of a will remain unchanged
so a = (3*4 – 10) mod 3 = 2 mod 3 = 2
easy isnt it?
Another example
a = (-340) mod 60 So a = (60*6 – 340) mod 60 = (360-340) mod 60 = 20 mod 60 = 20
**************
To find bmodN, just keep adding N to b until the number is between 0 and N.
As an example, N=13,b=27. Add 13 to -27, you get -14, again you get -1, and again you get 12.
So, 27mod13=12.
A bit more generally, you might want to realize that amodN=a+kNmodN for any kN. That should help with your first question.
***************
A encontrar bmodN, sólo mantenga la adición de N a b hasta que el número está entre 0 y N.

Como un ejemplo, N=13,b=27. Añadir 13 a -27, consigue -14, de nuevo usted obtiene -1, y de nuevo consigue 12.

Así, 27mod13=12.

Un poco más generalmente, usted podría querer darse cuenta de que amodN=a+kNmodN para kN. Eso debería ayudar con tu primera pregunta.
***************
Based on the C99 Specification: a = (a / b) * b + a % b
We can write a function to calculate (a % b) = a - (a / b) * b!
int remainder(int a, int b)
{
    return a - (a / b) * b;
}
For modulo operation, we can have the following function:
int mod(int a, int b)
{
    int r = a % b;
    return r < 0 ? r + b : r;
}

My conclusion is (a % b) in C is a remainder operator and NOT modulo operator.