|
Forum Index : Microcontroller and PC projects : MMBasic bit operator's.... and two's compliment
| Author | Message | ||||
| DigitalDreams Regular Member Joined: 03/05/2025 Location: United KingdomPosts: 65 |
Trying to invert an 8-bit binary number and display the result. Dim as integer a A=57 Print bin$(a,8) Working as expected.... Now add A=inv(a) Print bin$(a,8) Works as expected (inverting all bits) YET Displays ALL 64 bits not the low 8 !?!? On the subject, do we have any commands to handle (convert, display) 8-bit two's compliment binary numbers or do we have to manually manipulate ?. Yes I'm trying to read and write two's compliment binary numbers on the i2c bus. Edited 2025-07-23 00:55 by DigitalDreams |
||||
| twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1769 |
do you mean something like that? a=INV(57) And 2^8-1 Regards Michael ‚My name is Ozymandias, king of kings Look on my works, ye Mighty, and despair!‘ Nothing beside remains. Round the decay Of that colossal wreck, boundless and bare The lone and ... |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11815 |
BIN2STR$ |
||||
| PhenixRising Guru Joined: 07/11/2023 Location: United KingdomPosts: 2076 |
I love these functions ![]() Edited 2025-07-23 02:45 by PhenixRising |
||||
| DigitalDreams Regular Member Joined: 03/05/2025 Location: United KingdomPosts: 65 |
BIN2STR$ Not entirely useful. I take it the i2c commands strip higher bits from 64 bit integers and only deal with the low 8 ? |
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 9108 |
Very likely. AFAIK the I2C system in MMBasic is strictly byte-orientated. If you wanted to send anything longer you would have to split into bytes first. You could send four elements of an array to send a 64-bit integer that way. Mick Zilog Inside! nascom.info for Nascom & Gemini |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11815 |
BIN2STR$ and STR2BIN are specifically designed for things like I2C communication Suppose you want to send a 16-bit signed integer to a I2C device a$=bin2str$(int16,myint) i2c send addr%,0,2,a$ If it wants the bytes the other way round a$=bin2str$(int16,myint,BIG) i2c send addr%,0,2,a$ Same when receiving from i2c - just use str2bin I wrote these functions specifically to make I2C comms to all sorts of devices easy. |
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 9108 |
Ah, so the I2C data is still sent as bytes, but as string characters rather than as elements of a single-dimensional array. :) Mick Zilog Inside! nascom.info for Nascom & Gemini |
||||
| DigitalDreams Regular Member Joined: 03/05/2025 Location: United KingdomPosts: 65 |
Suppose you want to send a 16-bit signed integer to a I2C device a$=bin2str$(int16,myint) i2c send addr%,0,2,a$ If it wants the bytes the other way round a$=bin2str$(int16,myint,BIG) i2c send addr%,0,2,a$ Same when receiving from i2c - just use str2bin I wrote these functions specifically to make I2C comms to all sorts of devices easy. Ah, I see !. Thanks for clarifying Peter.... |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |