Recent

Author Topic: Mathematical question  (Read 3353 times)

ad1mt

  • Full Member
  • ***
  • Posts: 199
    • Mark Taylor's Home Page
Mathematical question
« on: February 28, 2024, 09:41:40 am »
I'm working on functions for bitwise NOT, AND, OR, XOR in a big integer library.
I've implemented negative values with a boolean sign flag (not as twos-complement).
I'm thinking about how the boolean sign flag should be dealt with in these functions, and was wondering if there were any maths experts who had an opinion on this.
For example, is there a strict mathematical rule for calculating the value of
Code: Pascal  [Select][+][-]
  1. ( (-1) AND (+1) )
My intuition says to attempt the same operation on the boolean sign flags of the operands. So in the example above with the AND function, the result would be negative if both operand1 AND operand2 were both negative, otherwise the result would be positive. But that might not be correct.
Another idea is to say these functions make no sense when the signs are different, and that an overflow exception should be raised.
I tried asking Google, but it just gave me answers for the arithmetic operators plus, minus, multiply, etc.
« Last Edit: February 28, 2024, 09:59:46 am by ad1mt »

AlexTP

  • Hero Member
  • *****
  • Posts: 2406
    • UVviewsoft
Re: Mathematical question
« Reply #1 on: February 28, 2024, 09:45:24 am »
>For example, is there a strict mathematical rule for calculating the value of   ( (-1) AND (+1) )

no. there is no such rule.
because in math, AND (and others) are defined only for non-negative numbers, integer numbers >=0.

i studied math (in Ru institute) and math did not use negative numbers for AND OR etc.
operators were not defined for negative nums.

Zvoni

  • Hero Member
  • *****
  • Posts: 2329
Re: Mathematical question
« Reply #2 on: February 28, 2024, 09:46:21 am »
I'm working on functions for NOT, AND, OR, XOR in a big integer library.
I've implemented negative values with a boolean sign flag (not as twos-complement).
I'm thinking about how the boolean sign flag should be dealt with in these functions, and was wondering if there were any maths experts who had an opinion on this.
For example, is there a strict mathematical rule for calculating the value of
Code: Pascal  [Select][+][-]
  1. ( (-1) AND (+1) )
My intuition says to attempt the same operation on the boolean sign flags of the operands. So in the example above with the AND function, the result would be negative if both operand1 AND operand2 were both negative, otherwise the result would be positive. But that might not be correct. I tried asking Google, but it just gave me answers for all the operators except AND.
Depends...
if it's a logical AND, your sample would return True (with whichever bitwise representation)
if it's a bitwise AND it would return positive 1, since it only compares the single bits, and returns 1 for the bit, where both bits are 1 in both operands

EDIT: Ahh...well.. i didn't study math, so i have to believe Alex... :D
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

paule32

  • Full Member
  • ***
  • Posts: 208
Re: Mathematical question
« Reply #3 on: February 28, 2024, 09:49:39 am »
The Precedence is:

- pow
- div
- mul
- add
- sub

Then: you could set your negative Numbers in abs() - the absolute value which is:

(| -1 | AND | +1 |).

which is than (I don't know your logic of your Application, so you have decide your self):

(1 AND 1).

which is:

TRUE & TRUE.

which results in TRUE.  or:  1 & 1 = 1.

440bx

  • Hero Member
  • *****
  • Posts: 4053
Re: Mathematical question
« Reply #4 on: February 28, 2024, 09:53:18 am »
I'm working on functions for NOT, AND, OR, XOR in a big integer library.
This implies that you're dealing with bitwise operations, not logical operations.

if you assign 1 to indicate a number is negative and 0 to indicate it is positive then the standard logic tables apply, i.e, 1 and 0 = 0 (or TRUE and FALSE = FALSE.)  It's just a bitwise operation, nothing special about it.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ad1mt

  • Full Member
  • ***
  • Posts: 199
    • Mark Taylor's Home Page
Re: Mathematical question
« Reply #5 on: February 28, 2024, 09:58:12 am »
Depends...
if it's a logical AND, your sample would return True (with whichever bitwise representation)
if it's a bitwise AND it would return positive 1, since it only compares the single bits, and returns 1 for the bit, where both bits are 1 in both operands
It is bitwise operations I'm defining. I've edited my original post to make this clear. Logical operations would fail strict type checking.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11454
  • FPC developer.
Re: Mathematical question
« Reply #6 on: February 28, 2024, 09:59:13 am »
Depends...
if it's a logical AND, your sample would return True (with whichever bitwise representation)

 I agree with AlexTP, primarily it is an unsigned operation.

So while what you say is true, the switch to the bitwise representation is like a signed->unsigned type conversion that on non 2-complement machines might change the bit pattern. Also there is the potential issue of sign extension before that happens if both operand sizes are mismatched.

I think Pascal's sibling Modula2 wouldn't let you do it, since there those operations are only allowed for CARDINAL, and there are no implicit conversions between signed and unsigned.
« Last Edit: February 28, 2024, 12:12:46 pm by marcov »

paule32

  • Full Member
  • ***
  • Posts: 208
Re: Mathematical question
« Reply #7 on: February 28, 2024, 10:00:09 am »
@440bx

not exactly.
You don't need bitwise, it is bitwise.

Code: Pascal  [Select][+][-]
  1. var A: Byte; B: Byte;
  2. begin
  3.   A := -1;
  4.   B :=  1;
  5.   if (A) AND (B) then  // -1 AND 1  =>  FALSE & TRUE  =>  FALSE.
  6. end.

ad1mt

  • Full Member
  • ***
  • Posts: 199
    • Mark Taylor's Home Page
Re: Mathematical question
« Reply #8 on: February 28, 2024, 10:07:53 am »
Doing a bitwise operation on the boolean sign flag is definitely wrong in my opinion, because that breaks strict type checking rules.
From what Alex says, it appears to me that these operations make no sense with negative values.
So unless I hear differently, I think I will perform the operation on the unsigned (absolute) values, and raise an exception if any of the operands are negative.
The calling code can then choose to use the result and ignore the exception.

AlexTP

  • Hero Member
  • *****
  • Posts: 2406
    • UVviewsoft
Re: Mathematical question
« Reply #9 on: February 28, 2024, 10:13:15 am »
Quote
So unless I hear differently, I think I will perform the operation on the unsigned (absolute) values, and raise an exception if any of the operands are negative.
The calling code can then choose to use the result and ignore the exception.
It is reasonable choice.
exception for negative numbers for bit operators.

paule32

  • Full Member
  • ***
  • Posts: 208
Re: Mathematical question
« Reply #10 on: February 28, 2024, 10:18:30 am »
You don't need strict Exception Faults.
You can (as I don't know your Application logic) set ALL Values to ABS() - the absolute Value.
Since in mathematically Set's of natural Numbers IN consists only positive Digits (0 ... 9), the POV (Point Of View) are the positive mathematically Object's.

Fibonacci

  • Sr. Member
  • ****
  • Posts: 419
Re: Mathematical question
« Reply #11 on: February 28, 2024, 10:20:55 am »
Imho there is no negative value if it comes to bitwise operations. You are doing operations on bits. Minus sign is a bit in some numeric types. When doing and/or/xor, this is just a bit, not a sign, it doesnt matter.

4 byte "-1" is just FFFFFFFF (decimal 4294967295). You cant know if its negative value if you dont know how to treat this number, as signed or not.

Your "-1" is 32 "1" bits. Then you do "and 1", the result of this operation is 1.

paule32

  • Full Member
  • ***
  • Posts: 208
Re: Mathematical question
« Reply #12 on: February 28, 2024, 10:29:13 am »
@Fibonacci

right.
It depends on the Application Logic.
You know: Input Angle is Output Angle...

POV on natural Numbers Body ( IN ) or the partial Sum of the real Numbers Body ( IR ) are the positive Numbers (0 .. 9) without floats.

for ALL other cases (Bits) -1 Bit is 0 (see: Buffer overflow => 11 => 000).

XOR is used to set Registers/Values to 0.

Code: Pascal  [Select][+][-]
  1. Like:  xor rax, rax  ; which set's rax to 0

Zvoni

  • Hero Member
  • *****
  • Posts: 2329
Re: Mathematical question
« Reply #13 on: February 28, 2024, 11:46:05 am »
4 byte "-1" is just FFFFFFFF (decimal 4294967295).
How do you figure that?
TS wrote "... not as two-complements"

FFFFFFFF being "-1" is only for "two-complements" (as far as i understand stuff like that)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

paule32

  • Full Member
  • ***
  • Posts: 208
Re: Mathematical question
« Reply #14 on: February 28, 2024, 11:57:25 am »
@Zvoni

0xFFFFFFFE  =>  2 ^32 - 1  =>  2 ^31  =>  (positive Numbers: 0 .. 2 ^31)
0xFFFFFFFF  =>  2 ^32   => (negative Number Flag, where -1 is the Sign-Bit)

 

TinyPortal © 2005-2018