Post Reply 
Twos Complement
02-14-2023, 10:09 PM (This post was last modified: 02-14-2023 10:17 PM by akmarul.)
Post: #1
Twos Complement
Hello all. I am quite comfortable to change between bases (eg, dec hex and etc). But I am still scratching my head, how to convert negative decimal to hex or binary. For example, if decimal is -5, then hex should give me FB. How do I do this in HP Prime? Or maybe my hex number if FB (a twos complement number), and how do I convert that to negative decimal?
Find all posts by this user
Quote this message in a reply
02-15-2023, 07:58 AM
Post: #2
RE: Twos Complement
Note that two's complement conventionally needs a particular finite word width: -5 is hex FB only for an 8 bit word. Otherwise, it might be FFFFFFFB, or FFFB, or even B, among many other possibilities. (The same is true for one's complement.)

Possibly the question is how to set the word width for binary or decimal numbers on the Prime... and I don't know the answer to that one.
Find all posts by this user
Quote this message in a reply
02-15-2023, 08:36 AM
Post: #3
RE: Twos Complement
(02-15-2023 07:58 AM)EdS2 Wrote:  Possibly the question is how to set the word width for binary or decimal numbers on the Prime... and I don't know the answer to that one.

Binary integer settings can be controlled via these system variables:

Bits := x, where 1≤x≤64, sets the binary integer word size.
Signed := 0 for unsigned (default)
Signed := 1 for signed
Base := 0 for binary
Base := 1 for octal
Base := 2 for decimal
Base := 3 for hexadecimal (default)

N.B. All of the above info is in the built-in Help system. Press Vars, [Home], Settings, Integer, then press Help after highlighting Base or Bits or Signed.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
02-18-2023, 07:24 PM (This post was last modified: 02-20-2023 05:48 PM by gehakte_bits.)
Post: #4
RE: Twos Complement
This might help, it follows mostly the traditional 2sC methods around..
(added save Signed state for those that use Signed, and crop to the requested bit width.)
Code:

// 2023.0218 pretty-prime v0.3b
#pragma mode(separator(.,;) integer(h32))

// cmpl_2(#FBh,8) -> -5
// cmpl_2(#FFFBh,8) -> -5
// cmpl_2(#FBh,16) -> 251
// cmpl_2(-5,16) -> #FFFBh
// cmpl_2(5,16) -> #5h

//===============
// 2's Complement
//===============
EXPORT cmpl_2(x,m)
BEGIN
LOCAL s:=Signed;
  Signed:=0; 
  CASE 

    IF TYPE(x)=0 THEN                       // real to integer
      x:=IP(x);
      IF -2^(m-1)≤x<2^(m-1) THEN            // range check integer m sized
        x:=SETBITS(R→B(x,m));
      ELSE 
        x:=0;
      END;
    END;

    IF TYPE(x)=1 THEN                       // integer to real
      IF BITAND(x,2^(m-1)) THEN             // test msb bit neg flag
        x:=-1*(1+BITNOT(SETBITS(x,m-1)));   // (invert m-bits)+1 to real
      ELSE 
         x:=B→R(SETBITS(x,m));
      END;
    END;

  END;
  Signed:=s;
  RETURN x;
END;
Find all posts by this user
Quote this message in a reply
04-04-2024, 07:39 AM
Post: #5
RE: Twos Complement
(02-18-2023 07:24 PM)gehakte_bits Wrote:  This might help, it follows mostly the traditional 2sC methods around..
(added save Signed state for those that use Signed, and crop to the requested bit width.)
Code:

// 2023.0218 pretty-prime v0.3b
#pragma mode(separator(.,;) integer(h32))

// cmpl_2(#FBh,8) -> -5
// cmpl_2(#FFFBh,8) -> -5
// cmpl_2(#FBh,16) -> 251
// cmpl_2(-5,16) -> #FFFBh
// cmpl_2(5,16) -> #5h

//===============
// 2's Complement
//===============
EXPORT cmpl_2(x,m)
BEGIN
LOCAL s:=Signed;
  Signed:=0; 
  CASE 

    IF TYPE(x)=0 THEN                       // real to integer
      x:=IP(x);
      IF -2^(m-1)≤x<2^(m-1) THEN            // range check integer m sized
        x:=SETBITS(R→B(x,m));
      ELSE 
        x:=0;
      END;
    END;

    IF TYPE(x)=1 THEN                       // integer to real
      IF BITAND(x,2^(m-1)) THEN             // test msb bit neg flag
        x:=-1*(1+BITNOT(SETBITS(x,m-1)));   // (invert m-bits)+1 to real
      ELSE 
         x:=B→R(SETBITS(x,m));
      END;
    END;

  END;
  Signed:=s;
  RETURN x;
END;
Hello there! Converting negative decimal numbers to hexadecimal or binary can indeed be a bit perplexing at first, but fear not, it's entirely feasible. When dealing with negative numbers in hexadecimal or binary, the concept of two's complement comes into play.

Here's how you can convert a negative decimal number to hexadecimal using the two's complement method in HP Prime:

1. **Convert Decimal to Binary**: First, convert the negative decimal number to its binary representation. For instance, for -5, the binary representation is 1111 1011 (assuming 8-bit representation).

2. **Apply Two's Complement**: To find the two's complement, invert all the bits (change 0s to 1s and vice versa) and then add 1 to the result. So, for -5, the two's complement would be 0000 0101 + 1 = 0000 0110.

3. **Convert Binary to Hexadecimal**: Now, you can convert the two's complement binary number to hexadecimal. In this case, 0000 0110 translates to 06 in hexadecimal.

So, -5 in decimal is equivalent to 06 in hexadecimal.

If you have a hexadecimal number such as FB (assuming it's in two's complement form), and you want to convert it back to negative decimal:

1. **Convert Hexadecimal to Binary**: Convert the hexadecimal number to its binary equivalent. For FB, it's 1111 1011.

2. **Apply Two's Complement**: As FB is already in two's complement form, no need to invert and add 1. It represents a negative number directly.

3. **Convert Binary to Decimal**: Convert the binary representation to decimal. For FB, it translates back to -5 in decimal.

So, FB in hexadecimal is equivalent to -5 in decimal.

I hope this helps clarify the process for you! Let me know if you have any further questions.
Visit this user's website Find all posts by this user
Quote this message in a reply
04-04-2024, 02:30 PM
Post: #6
RE: Twos Complement
(04-04-2024 07:39 AM)phucshuhari Wrote:  So, -5 in decimal is equivalent to 06 in hexadecimal.
and, later
Quote:So, FB in hexadecimal is equivalent to -5 in decimal.

I'm confused.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)