Bitwise XOR operator to replace bitwise NOT operator?

Hi,
I need to invert the bits of an integer but I can't find the bitwise NOT operator.

I am trying the following alternative to get inverted the 28 lower bits.

Is this a valid alternative?

Thanks,
Osmany

Now that I realize, you could just subtract the number from a string of just 1s ((10^n)-1/9).

Do we have to worry about the integer being negative?

Here's some sample conversion code for you.
Note that AI2 binary 1 and 0 are text unless working with bitwise operators.

1 Like

Hi @ABG,

I was asking for validation because I don't know the internal representation of numbers in appinventor.

I am representing sets using bits of integers. The universe of elements are the numbers from 0 to 27. If, for example, bits 0 and 10 of an integer (representing a set) are active (0x00000401 as hexadecimal, 1025 as decimal), then the set is {0, 10}.

The complement of that set would be the bitwise NOT operator, it should be the set {0...27} - {0,10} which as a number would be (0xFFFFFBFE as hexadecimal, -1026 as decimal).

As you can see here, number could result negative.

I require the best possible performance, working with strings is not an option, nor with lists. Rewriting my example without using strings
blocks(8)

where the value 268435455 is 0x0FFFFFFF in hex.

Thanks @NishyanthKumar, i've rewrote my code as procedure to invert lower 28 bits. This operator works for that amount of bits.

BEST REGARDS,
Osmany

2 Likes

This is how far you are from bare metal:

from

Thanks @ABG,

I had already read that post, it doesn't say how many bits long an integer is, that way I could know what number I do an XOR with, so my alternative works in a general way.

Maybe @ewpatton could help why AppInventor doesn't provide the bitwiseNOT operator and and what alternative to use.

Thanks for your time,
Osmany

Someone could certainly implement one as Scheme provides a bitwise-not function over its IntNum type.

Since the negation block performs two's complement on the number, you should be able to do bitwise negation on integers using the following:

blocks - 2024-08-30T163956.263

2 Likes

Thank you very much @ewpatton,

That works very well. The inclusion of this operator should be proposed.
Best regards.
Osmany

But how many 1's are there in bitwisenot(0)?

1 Like

Hi @ABG,
That's the question.... How many bits long is an integer?
As many bits as the representation of the integer -1 needs or supports.
Maybe this is why bitwise-NOT operator is not implemented.

In my case I'm only analyzing the lower 28 bits, and the solution from @ewpatton works well.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.