Wish C / Java style bitwise operators

wonder

Expert
Licensed User
Longtime User
Can we have it the "old-school" way (| & ^ >> << ~)?

Take for example this expression:
Code: Copy
int rgb = ((r & 0x0ff) << 16) | ((g & 0x0ff) << 8) | (b & 0x0ff);

The B4X equivalent is terribly long:
Code: Copy
Dim rgb = Bit.Or(Bit.Or(Bit.ShiftLeft(Bit.And(r, 0x0ff), 16), Bit.ShiftLeft(Bit.And(g, 0x0ff), 8)), Bit.And(b, 0x0ff)) As Int
I'm not even sure if I got it right, such is the reduced readability... :(

Can we do something about it? :)
Many thanks in advance!

PS: It would be nice to have, at least, a parser. Something like this:
Code: Copy
Dim rgb = Bit.ParseString("((r & 0x0ff) << 16) | ((g & 0x0ff) << 8) | (b & 0x0ff)") As Int
 
Last edited:
I have had the same issue numerous times, the B4A notation is making simple expressions really convoluted, and unreadable. I wish the standard C notation was implemented since most other languages follow it.

Try to write this in B4A please and you will understand:
int binary = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16) | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff);
 
Can we have it the "old-school" way (| & ^ >> << ~)?
PS: It would be nice to have, at least, a parser. Something like this:
Code: Copy
Dim rgb = Bit.ParseString("((r & 0x0ff) << 16) | ((g & 0x0ff) << 8) | (b & 0x0ff)") As Int

Like this?
Code: Copy
Dim cb as Codeblock
cb.Compile("{|r,g,b|((r & 0x0ff) << 16) | ((g & 0x0ff) << 8) | (b & 0x0ff)}")
rgb1 = cb.Eval2( Array( 96, 53, 36 ) )
rgb2 = cb.Eval2( Array( 36, 37, 96 ) )
 
I'm working on the decompiler as we speak, but we should have the library posted later today. Source code will be maintained here:

https://github.com/macthomasengineering/mteeval-b4x-library

Currently the compiler supports basic math operators +-*/ but you could add bit shifting without too much drama.

You would tuck it in around here.

1_mteeval_evalunary_bitshift_zpslupfle1c.jpg


And then add the instructions to the machine.

2_mteeval_pcode_bitshift_zpsd8gs0nue.jpg
 
Last edited:
How do you get stuff compiled into machine code with B4A? :eek:

When you own the CPU, anything is possible. :)

3_mteeval_virtual_machine_zpsekh4hdtl.jpg


Decompiler is done too.

4_mteeval_decompile_zps58liweqj.jpg


I'm meeting a client soon. When I get back, I'll package all this up and post the library and source.
 
Wow!! So did you use B4A to create a virtual machine that runs on the Dalvik virtual machine, which in turn, can also run in my Android virtual machine on my real x86 machine PC???? :D

37670517.jpg


I can't wait to give it a test drive!! :)
 
@Erel, will you take this request into consideration? Using Bit.Something() is fine for simple operations, but when you need to read "a few" bits and combine "a few" bytes, it becomes a nightmare, in terms of readability.
 
Thank you! :)
I'm not asking for a replacement, only for an alternative syntax... This way, I believe beginners and experts would be both happy.

Regarding the "&" operator, hopefully it can be segregated into bitwise or string concatenation, depending on context, something like:
If betweenStrings or betweenChars Then IsConcatenator = TRUE
 
Back
Top