FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Operador Unary
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Operador Unary
Posted: Wed Dec 11, 2013 01:07 PM

Hola
Existe el operador Unario (Unary, ~ operador) en Harbour?
O alguien lo tiene construido desde un valor decimal o hexadecimal?

Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noci贸n del tiempo

El secreto de la felicidad no est谩 en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: Operador Unary
Posted: Wed Dec 11, 2013 01:56 PM
Bueno, a falta de pruebas exhaustivas creo que este c贸digo puede servir
Alguien que me lo pueda confirmar?

Code (fw): Select all Collapse
Function OperUnary( nNum )
Local nVal
Local cVal 聽 聽 聽:= ""
Local nLen 聽 聽 聽:= hb_BLen( DecToBin( nNum ) )
Local x
Local cNumBin 聽 := DecToBin( nNum )

// 聽? Valtype( DecToBin( 10 ) )

// ? HB_BITNOT( 5 ) // -6

For x = 1 to nLen
聽 聽 if Substr( cNumBin, x , 1 ) = "1"
聽 聽 聽 聽cVal 聽:= cVal + "0"
聽 聽 else
聽 聽 聽 聽cVal 聽:= cVal + "1"
聽 聽 endif
Next x
nVal 聽 := BinToDec( cVal )
//? cNumBin, nVal, cVal
Return nVal


Este link me ha sido de gran ayuda
http://harbouradvisor.blogspot.com.es/2 ... -bits.html
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noci贸n del tiempo

El secreto de la felicidad no est谩 en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 729
Joined: Tue Oct 18, 2005 06:49 PM
Re: Operador Unary
Posted: Fri Dec 13, 2013 09:47 AM
Cristobal,

Quizas esto te funcione:
Code (fw): Select all Collapse
#pragma BEGINDUMP
#include <hbapi.h>
#include <math.h>
HB_FUNC( C_COMPLEMENT )
{
聽 聽 long long int nDecimal1;
聽 聽 long long int nComplement;
聽 聽 nDecimal1 = hb_parnll(1);
聽 聽 nComplement = ~nDecimal1;
聽 聽 hb_retnll( nComplement );
}
#pragma ENDDUMP

Solo tienes que llamar la funcion C_COMPLEMENT(nDecimal)
Donde nDecimal es el numero, en formato decimal, al cual quiere hallar el complemento.
Ejemplo C_COMPLEMENT(5000), devuelve -5001

Saludos,

George
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: Operador Unary
Posted: Sun Dec 15, 2013 11:15 AM
George, gracias por tu interes
Eso mismo lo hace la funci贸n hb_bitNot
Code (fw): Select all Collapse
? hb_bitNot(5000)

Mi problema viene de convertir el -5001 a binario
Gracias de nuevo
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noci贸n del tiempo

El secreto de la felicidad no est谩 en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 729
Joined: Tue Oct 18, 2005 06:49 PM
Re: Operador Unary
Posted: Mon Dec 16, 2013 11:39 PM
Cristobal,
Trata convirtiendo el valor decimal primero a Hexadecimal con la funcion de Harbour NumToHex(nDecimal), luego usa la funcion siguiente: HexToBinary(cHex)
Code (fw): Select all Collapse
FUNCTION HexToBinary(cHex)
聽 聽 LOCAL nLenBin, nX, nPos, cBinNumber, cCharHex, cBinBase
聽 聽 aHexBinary 聽:= { 聽 聽{"0",聽 聽"0000"}, 聽 聽{"1", "0001"}, {"2",聽 聽 "0010"}, 聽{"3",聽"0011"}, {"4",聽 "0100"}, {"5", "0101"},;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 {"6",聽 聽"0110"}, 聽聽 {"7",聽 聽"0111"},聽 聽 {"8",聽 聽"1000"}, 聽{"9",聽"1001"},聽 聽 {"A",聽 聽"1010"}, {"B", "1011"},;
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 {"C",聽 聽"1100"},聽 聽 聽 聽 {"D", "1101"},聽 {"E",聽 聽"1110"}, 聽{"F",聽"1111"}}

聽 聽 cBinNumber := ""
聽 聽 cHex := alltrim(cHex)

聽 聽 FOR nX := len(cHex) 聽TO 1 STEP -1
聽 聽 聽 聽 聽cCharHex := substr(cHex, -nX, 1)
聽 聽 聽 聽 聽nPos := ascan(aHexBinary, { |a| a[1] == substr(cHex, -nX, 1)} )
聽 聽 聽 聽 聽cBinBase := aHexBinary[nPos,2]
聽 聽 聽 聽 聽cBinNumber := cBinNumber + cBinBase
聽 聽 NEXT

RETURN (cBinNumber)


Saludos,

George,

Continue the discussion