### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Mathematical question  (Read 3379 times)

#### Zvoni

• Hero Member
• Posts: 2333
##### Re: Mathematical question
« Reply #15 on: February 28, 2024, 11:59:49 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)
Yeah, but i was always under the impression, that FFFFFFFF for a signed 32-Bit Integer is −2,147,483,648
As i said: TS stated NOT as 2-complement!
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: 220
##### Re: Mathematical question
« Reply #16 on: February 28, 2024, 12:03:59 pm »
You right.
4 Byte = 4 Byte (each Byte has 8 Bit) multiply by 8 Bit = 32 Bit  ( 4 * 8 ).

0 .. 4 GB can be seen as full 32 Bit
-2 GB .. 2 GB can be seen as "two" half 32-Bit => 2 * 16 Bit - depend on Sign-Bit
« Last Edit: February 28, 2024, 12:06:01 pm by paule32 »

#### alpine

• Hero Member
• Posts: 1069
##### Re: Mathematical question
« Reply #17 on: February 28, 2024, 12:41:55 pm »
i studied math (in Ru institute) and math did not use negative numbers for AND OR etc.
operators were not defined for negative nums.
I'm quite curious how are they defined for positive numbers according to your studies there.
I got my degree from a BG university and I believe there shouldn't be much difference between BG/RU terminology at that time. We called them logical conjuction and disjunction, they were a set operators (in the border case with two elements in a set they become what we know as boolean operators) and never named them with OR and AND (but + and ., or ∨ and ∧).

As long for the: (-1) AND (1)
IMHO the most conventional way of calculating that is*: ((-1)<>0) AND ((1)<>0)

*)From the languages without a logic types.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

#### paule32

• Full Member
• Posts: 220
##### Re: Mathematical question
« Reply #18 on: February 28, 2024, 12:51:19 pm »

IMHO the most conventional way of calculating that is*: ((-1)<>0) AND ((1)<>0)
*)From the languages without a logic types.

Then, you have:
1. (-1) < 0 = TRUE .
2. (-1) > 0 = FALSE .

AND

3. (+1) < 0 = FALSE .
4. (+1) > 0 = TRUE .

Then you have (on AND):
A. TRUE  & FALSE = FALSE .   =>  1 & 0  => 0.
B. FALSE & TRUE  = FALSE .   =>  0 & 1  => 0.

C. FALSE & FALSE = FALSE .  =>  0 & 0  =>  0.

#### alpine

• Hero Member
• Posts: 1069
##### Re: Mathematical question
« Reply #19 on: February 28, 2024, 12:59:03 pm »
Then, you have:
1. (-1) < 0 = TRUE .
2. (-1) > 0 = FALSE .

AND

3. (+1) < 0 = FALSE .
4. (+1) > 0 = TRUE .

Then you have (on AND):
A. TRUE  & FALSE = FALSE .   =>  1 & 0  => 0.
B. FALSE & TRUE  = FALSE .   =>  0 & 1  => 0.

C. FALSE & FALSE = FALSE .  =>  0 & 0  =>  0.
What is your point?

There is no order in sets, i.e. no comparison operators. In Pascal there is, because the type is an enumeration:
Code: Pascal  [Select][+][-]
1. type Boolean=(false, true);
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

#### paule32

• Full Member
• Posts: 220
##### Re: Mathematical question
« Reply #20 on: February 28, 2024, 01:05:07 pm »
What is your point?
There is no order in sets, i.e. no comparison operators. In Pascal there is, because the type is an enumeration:
Code: Pascal  [Select][+][-]
1. type Boolean=(false, true);

That is true - Set's have no strict order.

But NOT on linear programming Condition's ( IF / WHEN ) :

WHEN (A IS FALSE) AND (B IS TRUE )  THEN  result := FALSE .    or:
WHEN (A IS TRUE ) AND (B IS FALSE)  THEN  result := FALSE .

#### ad1mt

• Full Member
• Posts: 199
##### Re: Mathematical question
« Reply #21 on: February 28, 2024, 03:31:07 pm »
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.
What you have said here only applies to integers in two's complement form.
My big integers are stored as absolute values, with a separate boolean sign flag.
Similarly with boolean types. In most implementations, boolean types are typcally an integer, with zero equal to FALSE, and non-zero equal to TRUE; and badly designed languages let you abuse type-checking rules in expressions.
It does not make much sense to action an XOR operation (e.g.) against two boolean values, where you do not have knowledge of the underlying representation.
« Last Edit: February 28, 2024, 05:28:45 pm by ad1mt »

#### Fibonacci

• Sr. Member
• Posts: 419
##### Re: Mathematical question
« Reply #22 on: February 28, 2024, 03:36:52 pm »
Yes, I kind of misunderstood what you wanted. You should show ppl what this is about so you can get more appropriate answers:

https://github.com/ad1mt/Multi-Word-Int/blob/main/src/Multi_Int.pas

#### ad1mt

• Full Member
• Posts: 199
##### Re: Mathematical question
« Reply #23 on: February 28, 2024, 03:44:15 pm »
Yes, I kind of misunderstood what you wanted. You should show ppl what this is about so you can get more appropriate answers:
As I said in my original post, I'm working on functions for bitwise NOT, AND, OR, XOR in a big integer library.
I was puzzled about how to deal with negative operands in calls to these functions; i.e. what the sign of the result should be when performing bitwise operations on negative numbers.
So I asked for the opinion of Mathematics experts to tell me what (if any) the "official rules" are.

#### Zvoni

• Hero Member
• Posts: 2333
##### Re: Mathematical question
« Reply #24 on: February 28, 2024, 04:11:56 pm »
Yes, I kind of misunderstood what you wanted. You should show ppl what this is about so you can get more appropriate answers:
As I said in my original post, I'm working on functions for bitwise NOT, AND, OR, XOR in a big integer library.
I was puzzled about how to deal with negative operands in calls to these functions; i.e. what the sign of the result should be when performing bitwise operations on negative numbers.
So I asked for the opinion of Mathematics experts to tell me what (if any) the "official rules" are.

Well, and as already said above by someone else: there is no negative in bitwise-operations. A bit is set or not
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

#### Fibonacci

• Sr. Member
• Posts: 419
##### Re: Mathematical question
« Reply #25 on: February 28, 2024, 04:12:17 pm »
Something is wrong here.

Maybe you should make separate types for signed (Multi_Int_XV) and unsigned (UMulti_Int_XV)?

The sign is there only if you want to see it, otherwise its just bits. You have "Bitwise operation on negative value" exceptions everywhere, this is wrong, there are no negative values for bitwise operations.

Multi_Int_XV = handle sign bit
UMulti_Int_XV = dont handle sign bit

Thats my suggestion.

Added new screenshot, instead of Integer I used Int64 (cos of overflow) to show that the value is incorrect after bitwise operation.
« Last Edit: February 28, 2024, 05:17:07 pm by Fibonacci »

#### ad1mt

• Full Member
• Posts: 199
##### Re: Mathematical question
« Reply #26 on: February 28, 2024, 05:43:08 pm »
Maybe you should make separate types for signed (Multi_Int_XV) and unsigned (UMulti_Int_XV)?
That is a good idea!
However, this big integer library that I started 3 years ago (in 2021), was meant to be a quick job, just to get another piece of code working, when I could not find an existing Pascal big integer library that was suitable and easy to use. Then I decided to tidy-up the code and donate the library to the Free Pascal community for possible inclusion as one of the "standard libraries". I started the tidying-up process approximately 1 year ago.
I'm now running out of energy on this project, and I've got other projects I want to start... making a men's shirt in the style of an American Bowling shirt, taking some extreme macro photo portraits of wasps, repairing the lining & pockets of my favourite leather jacket, etc.
If anyone else would like to take the big integer library code and improve it, feel free. I have made it public domain, with no license.
Regards, Mark Taylor
« Last Edit: February 28, 2024, 05:45:04 pm by ad1mt »

#### Fibonacci

• Sr. Member
• Posts: 419
##### Re: Mathematical question
« Reply #27 on: February 28, 2024, 05:54:50 pm »
Yeah unfortunately its not a quick small fix Good job so far, but these bitwise operations, they are wrong.

Taking macro photos of wasps sounds like fun

#### Thaddy

• Hero Member
• Posts: 14397
• Sensorship about opinions does not belong here.
##### Re: Mathematical question
« Reply #28 on: February 28, 2024, 06:05:19 pm »
Don't forget SAR and family if you adhere to a signed version.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

#### ad1mt

• Full Member
• Posts: 199
##### Re: Mathematical question
« Reply #29 on: February 28, 2024, 06:33:47 pm »
these bitwise operations, they are wrong.
Can you elaborate please?