Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:07 08 Sep 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MMBasic bit operator's.... and two's compliment

Author Message
DigitalDreams
Newbie

Joined: 03/05/2025
Location: United Kingdom
Posts: 31
Posted: 02:41pm 22 Jul 2025
Copy link to clipboard 
Print this post

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: Germany
Posts: 1621
Posted: 04:10pm 22 Jul 2025
Copy link to clipboard 
Print this post

  DigitalDreams said  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.

do you mean something like that?
a=INV(57) And 2^8-1

Regards
Michael
causality ≠ correlation ≠ coincidence
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10387
Posted: 04:22pm 22 Jul 2025
Copy link to clipboard 
Print this post

  Quote  Yes I'm trying to read and write two's compliment binary numbers on the i2c bus.


BIN2STR$
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1517
Posted: 04:44pm 22 Jul 2025
Copy link to clipboard 
Print this post

  matherp said  
  Quote  Yes I'm trying to read and write two's compliment binary numbers on the i2c bus.


BIN2STR$

I love these functions  
Edited 2025-07-23 02:45 by PhenixRising
 
DigitalDreams
Newbie

Joined: 03/05/2025
Location: United Kingdom
Posts: 31
Posted: 04:59pm 22 Jul 2025
Copy link to clipboard 
Print this post

  matherp said  
  Quote  Yes I'm trying to read and write two's compliment binary numbers on the i2c bus.


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 Kingdom
Posts: 8055
Posted: 05:19pm 22 Jul 2025
Copy link to clipboard 
Print this post

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
Preliminary MMBasic docs & my PCB designs
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10387
Posted: 05:44pm 22 Jul 2025
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 8055
Posted: 06:03pm 22 Jul 2025
Copy link to clipboard 
Print this post

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
Preliminary MMBasic docs & my PCB designs
 
DigitalDreams
Newbie

Joined: 03/05/2025
Location: United Kingdom
Posts: 31
Posted: 06:52pm 22 Jul 2025
Copy link to clipboard 
Print this post

  matherp said  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.


Ah, I see !. Thanks for clarifying Peter....
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025