Ones' Complement of Not 0 - Not quite technically correct?

Hi Everyone,

I am doing some work with MemoryBlocks and integers, and am getting different results from ‘Not 0’ when I use a MemoryBlock or when using an integer. Not zero in an integer of 1 is zero, while Not Zero of a memoryblock is 1111111111111111111111111111111111111111111111111111111111111110.

I understand that the One’s complement of the memory block should be 1111…etc…1111, and the last digit should probably be a 1 instead of a zero to have the number -0… am I correct, or did I mess up some code?.. :slight_smile:

Here is the code for using not 1 with an integer:

[code]Sub Action() Handles Action
//Create 2 integer variables
Dim X, Z as Integer
//Populate X with binary text data
X = Val("&b" + TFX.Text)

//Calculate Bit AND result
Z = Not X

//Show Bit Not result
LblAnswer.Text = Z.ToBinary
End Sub
[/code]

Here is the code for using not 1 with a MemoryBlock:

[code]Sub Action() Handles Action
//Create a MemoryBlock Variable
//64 bytes * 8 bits/byte = 512
Dim X as New MemoryBlock(512)

//Populate X with binary text data
X.UInt64Value(0) = Val("&b" + TFX.Text)

//Calculate Bit XOR result
Dim Z as New MemoryBlock(512)
Z.UInt64Value(0) = Not X.UInt64Value(0)

//Show Bit XOR result
LblAnswer.Text = Z.UInt64Value(0).ToBinary
End Sub
[/code]

The program can be downloaded here:

Example2-4.xojo_binary_project

Instinctively, Id say both these responses are wrong, although I can see how you might defend
Not 0 == 1

If doing a true NOT at a bit level,
Not 0 should get you a string of 1s as wide as the integer storage
So at bit level, you get 1
At byte level you get 11111111
and so on… but everything that was 0 should be 1

Yes, I agree Jeff. Glad to see that what I wrote made sense :slight_smile:

I’ll file a feedback report.

<https://xojo.com/issue/49007>

Mind you…the comments are a little bit misleading :slight_smile:

//Calculate Bit AND result
//Show Bit XOR result

Your right that the comments were misleading. I was calculating NOR and AND to make sure that those functions worked and I forgot to update the comments. Thanks for mentioning it.

You need to enforce 64bit by using UInt64 instead of using Integer. Then the results are identical. So no bug.

Here is the code:

Dim x As UInt64 = Val("&b" +TFX.Text) Dim z As UInt64 = Not x LblAnswer1.Text = z.ToBinary(64)
and:

[code]Dim x As New MemoryBlock(512)
x.UInt64Value(0) = Val("&b" +TFX.Text)

Dim z As New MemoryBlock(512)
z.UInt64Value(0) = Not x.UInt64Value(0)

LblAnswer.Text = z.UInt64Value(0).ToBinary(64)[/code]

Good to know, if slightly overcomplicated.
On a 64bit build, Integer should be 64bit anyway, yes?

10 house points to Eli

???

Yes. But this project is set to 32-bit.

Many thanks to @Eli Ott and @Robin Lauryssen-Mitchell as they both had the same answer.

I didn’t know that the value of 64 could be added to the ToBinary extension:

LblAnswer.Text = z.UInt64Value(0).ToBinary(64)

I always seem to be learning something new :slight_smile:

Just an added note for the logic clarity behind this question. I made a presumption in believing that because the integer value of ‘not 1 = 0’, then the binary value of 'not 0001 should mistakenly be ‘0000’ (binary representation of an integer), and instead the program correctly had the value of ‘not 0001 is 1110’