Union? of two binary arrays
Let's say we have two byte arrays:
I want to perform some operation on the arrays such that if the first byte in BOTH arrays is 0, then the resulting array's first byte is 0 but if EITHER byte is a 1, then the resulting array's first byte is a 1. (Extend this logic to each byte) Thus, the resulting array should be:
Is there any nice function that will do this for me?
27 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
So true, I miss linq when working with other langs 😅
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
|
is a bitwise OR
So basically the example above is
Return a or b
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
If ba1[1] is 0, yes the output would be 0
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
No, its return a or b
Let's take a Boolean example
var res = true or false
What's the value of res
?Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
|
operates on all numbers ofc, not just 01 and 00Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
So
1 | 2
would equal 3
But 1 | 1
equals 1
And 123 | 123
== 123
...etc
If it's still confusing 😅
bitwise operations operate on bitsUnknown User•3y ago
Message Not Public
Sign In & Join Server To View
No, that's not quite right 😅
byte a = 1
byte b = 3
A's binary
00000001
B's binary
00000011
The operations is basically this
00000001 | 00000011
, it operations on each bit individually, so least significant bits are 1, the result's least significant bit is 1 as well, we move on to the next one, in B it's 1 in A it's 0, we set it to 1 in the result.
The rest is 0 so it'll be set to 0 as well
So the result's binary would be
00000011
which is 3
In the case of a = 1
b = 2
A
00000001
B
00000010
Does it make sense now 😅 ?Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
No,
2 == b00000010
, 3 == b00000011
Do you know how binary numbers work?Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
That's okay 😅
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Yes, that's correct
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
bitwise operations are used a LOT in low level code
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Things like encryption, embedded systems, graphics
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
np