Binary Operations - I might just be too dumb...

M

Mike Bartels

Hi there,

I am trying to code a 'cloaking algorithm' that my Polar S720i heart rate
monitor puts on every byte that it sends or receives via infrared.
The logic is, that a payload byte array is sent through this algorithm byte
by byte.

This is the logic: B is the source byte, M the result byte

M = (((B + 0x100 - (1<<6)) & 0xff) & (1<<7)) |
(((B + 0x100 - (1<<5)) & 0xff) & (1<<6)) |
(((B + 0x100 - (1<<4)) & 0xff) & (1<<5)) |
(((B + 0x100 - (1<<3)) & 0xff) & (1<<4)) |
(((B + 0x100 - (1<<2)) & 0xff) & (1<<3)) |
(((B + 0x100 - (1<<1)) & 0xff) & (1<<2)) |
(((B + 0x100 - (1<<0)) & 0xff) & (1<<1)) |
!(B & 1)

So if B is 0x00 the following should happen (and also does on a piece of
paper!)

M = (192 & 128) |
(224 & 64) |
(240 & 32) |
(248 & 16) |
(252 & 8) |
(254 & 4 ) |
(255 & 2 ) |
!(0 & 1) = 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 = 0xff

I have programmed it this way:

resultByte[position] =
(((B + 0x100 - (1<<6)) & 0xff) & (1<<7)) |
(((B + 0x100 - (1<<5)) & 0xff) & (1<<6)) |
(((B + 0x100 - (1<<4)) & 0xff) & (1<<5)) |
(((B + 0x100 - (1<<3)) & 0xff) & (1<<4)) |
(((B + 0x100 - (1<<2)) & 0xff) & (1<<3)) |
(((B + 0x100 - (1<<1)) & 0xff) & (1<<2)) |
(((B + 0x100 - (1<<0)) & 0xff) & (1<<1)) |
!(B & 1);

Jetzt gibt es aber mit dem !(B & 1) in der letzen Zeile probleme. Wenn ich
compilieren will, sagt er mir "Operator '!' cannot be applied to operand of
type 'int'.
Well, of course the last line "! (B & 1);" causes the compiler to act up.
"Operator '!' cannot be applied to operand of type 'int'.

I am not a binary person (never did C++, so forgive me) :D

In my innocence I interpret "B & 1" as "0x00 & 0x01" (with the example of B
= 0x00). I would expect the result to be 0x00 (right?). 0x00 is 0 (int) and
this should be made a 1 through the not ('!') operator. But really I want a
0x01, so the last '|' operator does not tell me that he can't implicitly
convert 'int' to 'byte'.

I understand that if B is anything else but 0 I have problems anyways... But
WHAT exactly can I do?

Please help!

Kind regards,
Michael Bartels
 
C

Christoph Nahr

But really I want a
0x01, so the last '|' operator does not tell me that he can't implicitly
convert 'int' to 'byte'.

I haven't analyzed those huge binary operations but I think this one's
easy: ! is a boolean operator, not a binary operator. What you're
looking for is the ~ operator which does a bitwise complement.
 
M

Mike Bartels

Thank you, yes that ~ helped (a little ;) )

Now it tells me that it cannot implicitly convert 'int' into 'byte'. It does
not seem to like the '|' operator anymore.

resultByte[position] =
(((B + 0x100 - (1<<6)) & 0xff) & (1<<7)) |
(((B + 0x100 - (1<<5)) & 0xff) & (1<<6)) |
(((B + 0x100 - (1<<4)) & 0xff) & (1<<5)) |
(((B + 0x100 - (1<<3)) & 0xff) & (1<<4)) |
(((B + 0x100 - (1<<2)) & 0xff) & (1<<3)) |
(((B + 0x100 - (1<<1)) & 0xff) & (1<<2)) |
(((B + 0x100 - (1<<0)) & 0xff) & (1<<1)) |
~(B & 1);

I have tried casting the values with a (byte) but it won't let me do that.
Does ~(B & 1) result in an int
value?

I wish I was a binary rocket scientist... I should have learned C++ instead
of Basic :p

Mike
 
J

Jon Skeet [C# MVP]

Mike Bartels said:
Thank you, yes that ~ helped (a little ;) )

Now it tells me that it cannot implicitly convert 'int' into 'byte'. It does
not seem to like the '|' operator anymore.

resultByte[position] =
(((B + 0x100 - (1<<6)) & 0xff) & (1<<7)) |
(((B + 0x100 - (1<<5)) & 0xff) & (1<<6)) |
(((B + 0x100 - (1<<4)) & 0xff) & (1<<5)) |
(((B + 0x100 - (1<<3)) & 0xff) & (1<<4)) |
(((B + 0x100 - (1<<2)) & 0xff) & (1<<3)) |
(((B + 0x100 - (1<<1)) & 0xff) & (1<<2)) |
(((B + 0x100 - (1<<0)) & 0xff) & (1<<1)) |
~(B & 1);

I have tried casting the values with a (byte) but it won't let me do that.

I suggest you just cast the result to a byte.
Does ~(B & 1) result in an int
value?

Yes. Pretty much all operations with operands int or smaller result in
an int.
I wish I was a binary rocket scientist... I should have learned C++ instead
of Basic :p

LOL :)
 
M

Mike Bartels

Maybe I should visit a casting show :p

Look at this code:

byte B = 0x00;
byte A,C,D = new byte();

A = (byte)(((B + 0x100 - (1<<6)) & 0xff) & (1<<7)); // = (byte(0 + 256 -
64) & 255) & (128) = 192 &128 = 128
C = (byte)(((B + 0x100 - (1<<5)) & 0xff) & (1<<6)); // = 244 & 64 = 64

D = A | C; // ERROR

Now both, A and C, are of byte value, I assume. So why does D = A | C;
result in 'Cannot implicitly convert type 'int' to 'byte'??? *cry*

Its not to late to move back to VB, BUT I guess binary operation are no fun
there, either ;)

Mike
 
D

David Dawkins

In my innocence I interpret "B & 1" as "0x00 & 0x01" (with the example of B
= 0x00). I would expect the result to be 0x00 (right?). 0x00 is 0 (int) and
this should be made a 1 through the not ('!') operator. But really I want a
0x01, so the last '|' operator does not tell me that he can't implicitly
convert 'int' to 'byte'.

I understand that if B is anything else but 0 I have problems anyways... But
WHAT exactly can I do?

In C/C++ you are permitted to treat int values as bool[ean] values,
with 0 as false and everything else as 1.

In C# (and Java), the bool and int types are completely distinct.

So, where in C++ you'd write

!(B & 1)

in C# you will need to write

((B&1) == 1) ? 0 : 1

Dave D
 
J

Jon Skeet [C# MVP]

Mike Bartels said:
Maybe I should visit a casting show :p

Look at this code:

byte B = 0x00;
byte A,C,D = new byte();

A = (byte)(((B + 0x100 - (1<<6)) & 0xff) & (1<<7)); // = (byte(0 + 256 -
64) & 255) & (128) = 192 &128 = 128
C = (byte)(((B + 0x100 - (1<<5)) & 0xff) & (1<<6)); // = 244 & 64 = 64

D = A | C; // ERROR

Now both, A and C, are of byte value, I assume. So why does D = A | C;
result in 'Cannot implicitly convert type 'int' to 'byte'??? *cry*

Because as I said, binary operations with operands of size int or
smaller result in an int. You just need to cast to byte again:

D = (byte) (A|C);
Its not to late to move back to VB, BUT I guess binary operation are no fun
there, either ;)

Not sure, to be honest.
 
M

Mike Bartels

Doh!

D = (byte)(A | C);

Did you realize that there was no such thing as casting in VB? Variable data
type *sigh*!
 
M

Mike Bartels

Thank you!

That solved it - I had an if statement to get to the same result, but your
code is much nicer!

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top