Binary manipulations

guys,
i wanna know how to directly interact with binary values, lets say for example int 78.
i dont wanna print or deal with ascii representation of the binary as "0" and "1"
is that possible?
then how?
thanks

void setup() {
  Serial.begin(115200);

  int value = 78;

  Serial.print("Result: ");
  Serial.print(value, BIN);
}

void loop() {
}

Result: 1001110
1 Like

It is possible.

Can you provide an example of what you are trying to do?

For example, 78 (decimal) is encoded internally as a binary number, such as 0b1001110, or 0b00000001001110 using 16 bits. You can work with this.

Try reading the Arduino documentation in https://www.arduino.cc/reference/en/ regarding bitwise operations.

2 Likes

See more capabilities of Serial.print() here:

1 Like

What does "directly interact" mean?

1 Like
2 Likes

So, What ?? You're then trying to interact with the machine code,
even then, it can be binary or can be Hex, it depends
I have a friend who did this for some 3 years and i did it with him.

it's cool when you start it, you learn a lot
it then turns your brain to aeroplane Jelly LOL

You sure you want to go down that road.
it's not for everyone

1 Like

Those are just two ways to write the same thing. They only matter at the point numbers are being shown to humans. Inside it's all binary.

If I'm coding something at high speed or tight timing requirements and want to set pins 5, 6, and 7 HIGH at the same time I can (assuming I have DDRD already set correctly):

PORTD = 0b11100000;

Alternatively I can:
PORTD = 0xE0;

Or I can even use decimal if I want:
PORTD = 224;

and all three lines are exactly equivalent and compile to exactly the same code.

And last but not least, this one ends up the same after compilation but gets written a little differently. Lots of times you'll have the 5, 6, and 7 named or defined as something and this way you can put those together.

// define pin numbers to bit masks
// and again these don't have to be hex it's just easier to write that way. 
const int PIN5 = 0x20;
const int PIN6 = 0x40;
const int PIN7 = 0x80;

PORTD = (PIN5 | PIN6 | PIN7);

1 Like

No i understand that, i was talking about the Native machine code

well of course because a human has to look at the code but if you want to go that low, then it's not even binary , it's just electrical signals ON or OFF the word BINARY doesn't even exist to the computer it just understands the state of the signal and digital logic.

but hey.. let's not go down that rabbit hole now LOL
it's a cool hole, I've been there, but i've been up all night and am looking to get some rest

in any case, i was just referring to Native Machine language that's all

on a side note
When me and my mate did this we started with a Z80 if for no other reason but to just understand how computers work and how CPU's work
later we went to a 6502 and that was also cool
many long hours, many many coffee's later and god knows how much debugging

DO I REGRET IT...........??
NOT FOR A SECOND
lol
IT WAS COOL
it's just not for everyone

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.