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 8 1648
On Sat, 22 May 2004 14:53:51 GMT, "Mike Bartels"
<ne**@news.t-online.de> wrote: 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.
-- http://www.kynosarges.de
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
Mike Bartels <ne**@news.t-online.de> wrote: 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 :)
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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
> 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
Mike Bartels <ne**@news.t-online.de> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Doh!
D = (byte)(A | C);
Did you realize that there was no such thing as casting in VB? Variable data
type *sigh*!
Thank you!
That solved it - I had an if statement to get to the same result, but your
code is much nicer!
Michael This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Leslaw Bieniasz |
last post by:
Cracow, 20.09.2004
Hello,
I need to implement a library containing a hierarchy of classes
together with some binary operations on objects. To fix attention,
let me assume that it is a...
|
by: nephish |
last post by:
i have an interesting project at work going on. here is the challenge.
i am using the serial module to read data from a serial input.
it comes in as a hex. i need to make it a binary and compare it...
|
by: Matt McGonigle |
last post by:
Hi all,
Please help me out with this. Perhaps it is a dumb
question, but I can't seem to make it work. I am doing a
file conversion using an unformatted binary file for input
and outputting to...
|
by: pedagani |
last post by:
Dear comp.lang.c++,
I'm interested in knowing the general techniques used to handle large
binary files (>10GB) efficiently such as tweaking with filebuf , etc.
Reading chunk by chunk seems to be...
|
by: junaidnaseer |
last post by:
ok I know I posted this question previously and then I got a reply and
I realized that I had asked a really dumb question but now I realize
that my question wasn't that dumb at all !
I had asked...
|
by: vim |
last post by:
hello everybody
Plz tell the differance between binary file and ascii
file...............
Thanks
in advance
vim
|
by: nicolasg |
last post by:
does anyone know a module or something to convert numbers like integer
to binary format ?
for example I want to convert number 7 to 0111 so I can make some
bitwise operations...
Thanks
|
by: Registered User |
last post by:
I've read in a book:
<quote>
With a binary-mode stream, you can't detect the end-of-file by looking
for EOF, because a byte of data from a binary stream could have that
value, which would...
|
by: nguser3552 |
last post by:
Hello everyone,
I'm wondering if someone out there knows how in a visual c++ console
application how I can do the following, and man I've tried, it seems simple
really:
I need to open up any...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |