C
C#•3y ago
Matt

Union? of two binary arrays

Let's say we have two byte arrays:
byte[] ba1 = { 01, 01, 00, 00 };
byte[] ba2 = { 01, 00, 01, 00 };
byte[] ba1 = { 01, 01, 00, 00 };
byte[] ba2 = { 01, 00, 01, 00 };
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:
byte[] baRes = { 01, 01, 01, 00 }
byte[] baRes = { 01, 01, 01, 00 }
Is there any nice function that will do this for me?
27 Replies
Kouhai
Kouhai•3y ago
var res = ba1.Zip(ba2, (a, b) => a | b).ToArray();
var res = ba1.Zip(ba2, (a, b) => a | b).ToArray();
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
So true, I miss linq when working with other langs 😅
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
| is a bitwise OR So basically the example above is Return a or b
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
If ba1[1] is 0, yes the output would be 0
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
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
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
| operates on all numbers ofc, not just 01 and 00
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
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 bits
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
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
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
No, 2 == b00000010, 3 == b00000011 Do you know how binary numbers work?
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
That's okay 😅
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
Yes, that's correct
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
bitwise operations are used a LOT in low level code
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
Things like encryption, embedded systems, graphics
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Kouhai
Kouhai•3y ago
np blobthumbsup
Want results from more Discord servers?
Add your server