Mathematical question lazarus / 21-General ---------------------------------------------------------------------- ad1mt · 2024-02-28 08:41 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ( (-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. ---------------------------------------------------------------------- AlexTP · 2024-02-28 08:45 >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 · 2024-02-28 08:46 Quote from: ad1mt on February 28, 2024, 09:41:40 amI'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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ( (-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... ---------------------------------------------------------------------- paule32 · 2024-02-28 08:49 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 · 2024-02-28 08:53 Quote from: ad1mt on February 28, 2024, 09:41:40 amI'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. ---------------------------------------------------------------------- ad1mt · 2024-02-28 08:58 Quote from: Zvoni on February 28, 2024, 09:46:21 amDepends... 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 · 2024-02-28 08:59 Quote from: Zvoni on February 28, 2024, 09:46:21 amDepends... 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. ---------------------------------------------------------------------- paule32 · 2024-02-28 09:00 @440bx not exactly. You don't need bitwise, it is bitwise. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var A: Byte; B: Byte;begin  A := -1;  B :=  1;  if (A) AND (B) then  // -1 AND 1  =>  FALSE & TRUE  =>  FALSE.end. ---------------------------------------------------------------------- ad1mt · 2024-02-28 09:07 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 · 2024-02-28 09:13 QuoteSo 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 · 2024-02-28 09:18 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 · 2024-02-28 09:20 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 · 2024-02-28 09:29 @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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Like:  xor rax, rax  ; which set's rax to 0 ---------------------------------------------------------------------- Zvoni · 2024-02-28 10:46 Quote from: Fibonacci on February 28, 2024, 10:20:55 am4 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) ---------------------------------------------------------------------- paule32 · 2024-02-28 10:57 @Zvoni 0xFFFFFFFE  =>  2 ^32 - 1  =>  2 ^31  =>  (positive Numbers: 0 .. 2 ^31) 0xFFFFFFFF  =>  2 ^32   => (negative Number Flag, where -1 is the Sign-Bit) ---------------------------------------------------------------------- Zvoni · 2024-02-28 10:59 Quote from: paule32 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) 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! ---------------------------------------------------------------------- paule32 · 2024-02-28 11:03 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 ---------------------------------------------------------------------- alpine · 2024-02-28 11:41 Quote from: AlexTP on February 28, 2024, 09:45:24 ami 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. ---------------------------------------------------------------------- paule32 · 2024-02-28 11:51 Quote from: alpine on February 28, 2024, 12:41:55 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 · 2024-02-28 11:59 Quote from: paule32 on February 28, 2024, 12:51:19 pmThen, 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type Boolean=(false, true); ---------------------------------------------------------------------- paule32 · 2024-02-28 12:05 Quote from: alpine on February 28, 2024, 12:59:03 pmWhat 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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 · 2024-02-28 14:31 Quote from: Fibonacci on February 28, 2024, 10:20:55 amWhen 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. ---------------------------------------------------------------------- Fibonacci · 2024-02-28 14:36 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 · 2024-02-28 14:44 Quote from: Fibonacci on February 28, 2024, 03:36:52 pmYes, 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 · 2024-02-28 15:11 Quote from: ad1mt on February 28, 2024, 03:44:15 pmQuote from: Fibonacci on February 28, 2024, 03:36:52 pmYes, 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 ---------------------------------------------------------------------- Fibonacci · 2024-02-28 15:12 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. ---------------------------------------------------------------------- ad1mt · 2024-02-28 16:43 Quote from: Fibonacci on February 28, 2024, 04:12:17 pmMaybe 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 ---------------------------------------------------------------------- Fibonacci · 2024-02-28 16:54 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 · 2024-02-28 17:05 Don't forget SAR and family if you adhere to a signed version. ---------------------------------------------------------------------- ad1mt · 2024-02-28 17:33 Quote from: Fibonacci on February 28, 2024, 05:54:50 pmthese bitwise operations, they are wrong. Can you elaborate please? ---------------------------------------------------------------------- Thaddy · 2024-02-28 17:36 He simply means that bits are bits, Not flags. ---------------------------------------------------------------------- Fibonacci · 2024-02-28 17:36 Quote from: ad1mt on February 28, 2024, 06:33:47 pmQuote from: Fibonacci on February 28, 2024, 05:54:50 pmthese bitwise operations, they are wrong. Can you elaborate please? Just look at the second screenshot. "-1" manipulated as Integer vs Multi_Int_XV. Integer is OK, Multi_Int_XV produces wrong result. Edit: Sorry, I forgot to mention I commented out the exception code in the unit Just that disallowing bitwise operations on negative values is wrong. It shouldnt be checked at all if its negative or positive. Possible solutions: 1) Dont store "NEGATIVE" boolean flag, instead store "SIGNED" flag 2) Separate types for signed and unsigned ---------------------------------------------------------------------- paule32 · 2024-02-28 17:43 but Bit's can be Flag's: o o o o o o o o  <--- Flag's (all set)  => 255 7 6 5 4 3 2 1 0  <--- Bit's (Position) => o       o o o    o  <--- Flag's (not all set) - can be use for: (Carry Flag, ..) 7 6 5 4 3 2 1 0  <--- Bit's ---------------------------------------------------------------------- ad1mt · 2024-03-01 09:46 Quote from: Fibonacci on February 28, 2024, 06:36:54 pmJust that disallowing bitwise operations on negative values is wrong. It shouldnt be checked at all if its negative or positive. I still don't understand what the problem is. To make it work, the programmer just has to do ABS on the value, like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i V:= Abs(W) << 1;However, if the programmer inadvertently tries to do a bitwise operation on a negative value, and would want that to be flagged as invalid, then what I'm doing is correct. If I "fix" the programmer's error, by doing an implicit ABS, that might not be what the programmer wanted, and could cause problems later like undiscovered bugs. The way I am doing it means the programmer has control. ---------------------------------------------------------------------- Fibonacci · 2024-03-01 10:00 So if I want to change 1 bit, you suggest to first call Abs() on the value? ---------------------------------------------------------------------- paule32 · 2024-03-01 11:19 for usually, I use Assembly Code for that. You can then do: asm   mov al, 0b   ; set first (0) bit to AL register   mov al, 1b   ; set first (1) bit to AL register   mov al, 10b ; set second bit (1) to AL register, and first bit (0) <-- remark's end; remarks: 00 = 0 01 = 1 10 = 2 so: asm mov al, 10b  end;  is the same as: asm mov al, 0x02 end; you can check the flag with:  JZ = Jump Zero (0), ... asm @start:   mov al, 00b   jz @zero   ; do something, if not zero @zero:   ; do something else, if AL is zero end; ---------------------------------------------------------------------- Thaddy · 2024-03-01 11:25 That is fine on 8 bit computers but is slow on anything else. ---------------------------------------------------------------------- paule32 · 2024-03-01 11:33 This was only an Example. It give even more Menomic's like: ADC = Adjust Carry. modern IBM compatible PC's have these old Instruction Set's for backward compatible with more or lesser complex other Menomic's. So, modern PC's are indeed very slow - in Context of RISC CPU's. Because: AL/AH will be store into AX AX   will be store into EAX, and EAX will be store into RAX This have to do with the Bandwidth of the BUS System. ---------------------------------------------------------------------- Fibonacci · 2024-03-01 11:49 @paule32: We are talking about Multi_Int_XV type here, not simple integers. 11111111111111111111111111111111 <- this is Integer(-1) 00000000000000000000000000000001 <- this is Abs(Integer(-1)) So should I call Abs() before doing bitwise operations? I guess not. If I want to change the sign, I can do it like this: int := int xor (1 shl 31) If the type is not Integer but Multi_Int_XV, I cant do this because "you cant do bitwise operations on negative values". Of course if I first call Abs() on the value, it will work, but the value will be completely wrong. @ad1mt: Again, you can know the real decimal value of the "bits" only if you know how to treat the bits, as signed or not. Currently you store a boolean flag to tell if this value is "negative" or not. Instead, you should store if its SIGNED or not. Bitwise operations operate on bits, the decimal value doesnt matter, if negative, above 100, equal to 33 or whatever. I didnt read all the code in your 15k LOC unit, I dont know how it actually works. The "decimal value" when returned as String should be built knowing if the expected value is signed or unsigned, not if its negative or not. Also I dont know why changing 1 bit (the most significant bit in 32bit value) causes "overflow" exception when converting it back to 4 byte Integer. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  i: integer;  m: Multi_Int_XV;begin  i := 1;  m := i; // 4 bytes  m := m xor (1 shl 31); // still 4 bytes  i := m; // overflow exception, but its still 4 bytes If I touch the "sign bit", its overflow. ---------------------------------------------------------------------- paule32 · 2024-03-01 12:00 For Decision's, on negative or positive Number's, I use a Remainder carry Flag. So, in these there, you have three Options with two Bit's if carry = 00b then // do positive Action's for unsigned Value's if carry = 01b then // do negative Action's for signed Value's if carry = 10b then // abs(carry)  // then carry is always "unsigned" All is depend on you Application Logic, and Goal. With the Option's above, you ship around the Limit's of some Type's. So, you can have a the Size of a signed DWORD like -4 GB, and +4 GB instead only: -2 GB, and +2GB. Okay, this have a little payload. But this was only a thinking over Idea from me ... ---------------------------------------------------------------------- Thaddy · 2024-03-01 12:03 Quote from: paule32 on March 01, 2024, 12:33:49 pmThis have to do with the Bandwidth of the BUS System. No it has to do with the native width of the processor. There are efficient instructions, though, on e.g. Intel, like movb and family, to handle byte sized instructions. You should use those. ---------------------------------------------------------------------- paule32 · 2024-03-01 12:09 I am not a Friend of AT&T Assembly Style like: movb I loved, and prefer Intel Assembly Style like: mov byte ptr mov.b sounds like Motorolla 68k Assembly for me ---------------------------------------------------------------------- marcov · 2024-03-01 14:47 Quote from: paule32 on March 01, 2024, 12:19:07 pmyou can check the flag with:  JZ = Jump Zero (0), ... asm @start:   mov al, 00b   jz @zero Afaik mov doesn't affect the flags, so this code doesn't look so good. Binary notation for literals is a % in FreePascal, hex is $ and octal is &, all prefixes and including its assembler syntax. ---------------------------------------------------------------------- paule32 · 2024-03-01 16:09 you right. To correct this: mov al, 0 cmp al, 0 jz @zero ... Intel nasm differs from AT&T gas: Intel:  mov eax, 1        (dst <- src) AT&T: movl $1, %eax  (src -> dst) My fault - sorry. inline: movl $1, %%eax ---------------------------------------------------------------------- ad1mt · 2024-03-02 17:31 Quote from: Fibonacci on March 01, 2024, 12:49:18 pmI dont know why changing 1 bit (the most significant bit in 32bit value) causes "overflow" exception when converting it back to 4 byte Integer. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  i: integer;  m: Multi_Int_XV;begin  i := 1;  m := i; // 4 bytes  m := m xor (1 shl 31); // still 4 bytes  i := m; // overflow exception, but its still 4 bytesI was worried that my code might have a bug; and I always welcome bug reports. So I ran your code, to see what was going on. After the XOR operation, m = 2147483649 32-bit integer type is signed, and can only accept values between -2,147,483,648 and 2,147,483,647 2147483649 is outside its range, so assigning m to i correctly causes overflow. If you declare i to be unsigned integer (aka longword, aka uint32), then it works ok, because it can accept values from 0 to 4294967295 ---------------------------------------------------------------------- ad1mt · 2024-03-02 17:55 Quote from: Fibonacci on March 01, 2024, 11:00:04 amSo if I want to change 1 bit, you suggest to first call Abs() on the value? Yes, that's correct. It might seem inefficient, but if you run the code with a timer, the call to Abs adds very little overhead. ---------------------------------------------------------------------- Fibonacci · 2024-03-02 18:04 Quote from: ad1mt on March 02, 2024, 06:31:18 pmI was worried that my code might have a bug; and I always welcome bug reports. So I ran your code, to see what was going on. After the XOR operation, m = 2147483649 32-bit integer type is signed, and can only accept values between -2,147,483,648 and 2,147,483,647 2147483649 is outside its range, so assigning m to i correctly causes overflow. If you declare i to be unsigned integer (aka longword, aka uint32), then it works ok, because it can accept values from 0 to 4294967295 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  i: integer;  m: Multi_Int_XV;begin  writeln('xor on integer = ok');  i := 1;  writeln(binstr(i, 32), ' = ', i);  i := i xor (1 shl 31);  writeln(binstr(i, 32), ' = ', i);   writeln;   writeln('xor on integer then casted to Multi_Int_XV = ok');  i := 1;  m := i;  writeln(binstr(integer(m), 32), ' = ', string(m));     i := i xor (1 shl 31);  m := i;  writeln(binstr(integer(m), 32), ' = ', string(m));   writeln;   writeln('xor on Multi_Int_XV = overflow on cast to integer');  m := 1;      writeln(binstr(integer(m), 32), ' = ', string(m));  m := m xor (1 shl 31);  //writeln(binstr(integer(m), 32), ' = ', string(m)); // integer(m) = overflow  // because of overflow, lets cast it to int64  writeln(binstr(int64(m), 32), ' = ', string(m)); // high 32 bits are 0s   writeln;   // is the value the same as integer?  writeln('value correct? ', int64(m) = i, ', ', int64(m), ' vs ', i);      writeln('bits the same? ', binstr(i, 64) = binstr(int64(m), 64));  writeln('i bits = ', binstr(i, 64));  writeln('m bits = ', binstr(int64(m), 64));   writeln;  writeln('------------------------');  writeln;   writeln('negative value, test on integer');  i := -1;  i := i xor (1 shl 31);  writeln('i = ', i, ', bits = ', binstr(i, 32));   writeln('negative value, test on Multi_Int_XV');  m := -1;  //m := m xor (1 shl 31); // failed: Raise EInterror.create('Bitwise operation on negative value'); Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i xor on integer = ok00000000000000000000000000000001 = 110000000000000000000000000000001 = -2147483647 xor on integer then casted to Multi_Int_XV = ok00000000000000000000000000000001 = 110000000000000000000000000000001 = -2147483647 xor on Multi_Int_XV = overflow on cast to integer00000000000000000000000000000001 = 110000000000000000000000000000001 = 2147483649 value correct? FALSE, 2147483649 vs -2147483647bits the same? TRUEi bits = 0000000000000000000000000000000010000000000000000000000000000001m bits = 0000000000000000000000000000000010000000000000000000000000000001 Quote from: ad1mt on March 02, 2024, 06:55:57 pmQuote from: Fibonacci on March 01, 2024, 11:00:04 amSo if I want to change 1 bit, you suggest to first call Abs() on the value? Yes, that's correct. It might seem inefficient, but if you run the code with a timer, the call to Abs adds very little overhead. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  i: integer;begin  i := -123;  writeln('bits                   = ', binstr(i, 32));  i := -123;  i := i xor (1 shl 31);  writeln('bits xored without ABS = ', binstr(i, 32));  i := -123;  i := abs(i);  i := i xor (1 shl 31);  writeln('bits xored    with ABS = ', binstr(i, 32)); Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i bits                 = 11111111111111111111111110000101bits xored without ABS = 01111111111111111111111110000101bits xored    with ABS = 10000000000000000000000001111011 You see what Abs() is doing? ---------------------------------------------------------------------- ad1mt · 2024-03-02 18:15 Quote from: Fibonacci on March 01, 2024, 12:49:18 pmOf course if I first call Abs() on the value, it will work, but the value will be completely wrong. You must code it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var m:Multi_Int_XV;// m is set to some unknown value by other codeif (negative(m)) then m := -(ABS(m) xor (1 shl 31))else m := (m xor (1 shl 31)); BTW... I just realised that I somehow "lost" the Negative function from the library code. I've just fixed that in the last 3 mins. Please download the latest version of the code to get Negative functions back. The following will still work with the previous version: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if (m.negative) then m := -(ABS(m) xor (1 shl 31))else m := (m xor (1 shl 31));Apologies for that mistake. ---------------------------------------------------------------------- Fibonacci · 2024-03-02 18:21 Is that mentioned in your documentation? If one wants to use this unit, one must first read it all carefully cos it doesnt work as expected The expected behavior in this xoring is to do overflow. Also exceptions when doing bitwise operations on negative values are unexpected. ---------------------------------------------------------------------- ad1mt · 2024-03-02 18:53 Quote from: Fibonacci on March 02, 2024, 07:21:51 pmIs that mentioned in your documentation? @Fibonacci Thanks for reminding me... I've just realised that since clarifying/changing how to do bitwise operations on negative values in this thread, I forgot to update the documentation. I will update it very shortly. ---------------------------------------------------------------------- ad1mt · 2024-03-03 10:40 @Fibonacci I've changed my mind. If I had enforced this from the beginning, my original division algorithm would not have been allowed! So I'm going to remove all the restrictions on bitwise operations. I will also document the internal structure of the big integers, so that programmers can decide for themselves whether/how to use the bitwise operators. The new version is not ready yet, but hopefully will be ready by the end of today (Sunday). ---------------------------------------------------------------------- ad1mt · 2024-03-03 12:31 Here is my proposed logic for deciding the sign of the result when negative values are used in bitwise operations: XOR - if the signs are different, the result is negative, otherwise the result is positive OR - if the both signs are negative, the result is negative, otherwise the result is positive AND - if the both signs are negative, the result is negative, otherwise the result is positive NOT - if the sign is positive, the result is negative, otherwise the result is positive I propose to "give preference" to positive values in the result. This has resulted in a logical inconsistency between AND & OR operators. Maybe the behaviour of the AND & OR operators, should be "opposite" to each other, as follows... either OR - if the both signs are negative, the result is negative, otherwise the result is positive AND - if the both signs are positive, the result is positive, otherwise the result is negative (a positive sign is considered to a positive "value") or OR - if the both signs are positive, the result is positive, otherwise the result is negative AND - if the both signs are negative, the result is negative, otherwise the result is positive (a negative sign is considered to a positive "value") Alternative suggestions are welcome.