Decimal to binary

well x is an int (2 bytes on a UNO) and when you do x & 0xFF you just nullify the MSB, it's still two bytes. So if someone was to think the 2 bytes were going to be "sent to this port" (whatever that means, it's actually an assignment to a variable) then they would still think 2 bytes will be sent, with the MSB being forced to 0 - wouldn't they ?

Present computer saves my number 4567 as 0001000111010111 (0x11D7); I want to see it to have saved as 0100 0101 0110 0111. Can it be done in ATmega328P. I remember that 80286 supported BCD data type.

Why not --?
unsigned int y = 0b0100010101100111; //0100 0101 0110 0111

What I replied to with an FYI is

"// everything over 255 will be lost..."

Everything over what? That is not how binary works. It doesn't "fill up".

with functions

unsigned int decimalToBCD(unsigned int decimalNum) {
    unsigned int bcdResult = 0;
    unsigned int digitPosition = 0;

    while (decimalNum > 0) {
        unsigned int remainder = decimalNum % 10;
        unsigned int bcdDigit = remainder << (digitPosition * 4);
        bcdResult |= bcdDigit;
        decimalNum /= 10;
        ++digitPosition;
    }

    return bcdResult;
}

well, i don't know. I speak for myself. if OP mean "two bytes should be shown on port/s" then i say:

x=258;
PORTF=x>>8;
PORTK=x&0xFF;

Oh come on! You know that it can be done!

It requires converting the binary to 4 bit decimal digits packed 2 per byte.
I bet you have a big post prepared?

My 70's TI calculators ran on BCD. The processors were 4 bitters.

1 Like

Actually, I am still running after the reason why the RISC Processor (ATmegaxxx) has not included da/daa instruction in its Instruction Set when the said instruction was with 8085, 80x86, 8051 to adjust incorrect BCD result into correct BCD result after the addition of two BCD numbers.

Why aren't the Intel 4 bit compatibility CISC instructions also on the Atmel 8 bit RISC chips? ATmega cores don't need them or have legacy code?

Intel and their segmented memory is also due to backward compatibility.

The 6502 has 13 indirect addressing modes and operates directly on RAM.
THAT is something I would have liked.

1 Like

then why not just

x=258;
PORTF = x>>8;
PORTK = x;

yes, you are right.

This is a number using Roman numerals. I learnt it in 5th grade in the year 1960. Now, I am working out the representation of your Roman Number using 0 - 9.

M = 1000
D = 500
L = 50
X = 10
V = 5
I I = 2

MMMMDLXVII = 4000 + 500 + (50+10) + (5+2)
= 4567

Before I proceed to study and test your given sketch, I would like to know if the name of the function would be binaryToBCD so that ---

when 
input variable x = 0011000000111001 (for 12345 in binary format),
then
output variable y = 00010010001101000101 (for 12345 in BCD format)

I had some experience on 6802 of which 6502 is a kin. Why did you prefer to use the indirect addressing modes instead of direct addressing modes which are faster.

just call it unsigned int toBCD(unsigned int decimalNum)

it works (or should) on an unsigned int

next you can study

Nulla, I,II, III, IV,V, VII, VIII and IX (following the example by @GolamMostafa

1 Like

In Roman Number System, there are only seven symbols which can be added or subtracted to make any number.

1. I - 1
2. V - 5
3. X - 10
4. L - 50
5. C - 100
6. D - 500
7. M - 1000

there was no zero for the romans

but you have other rules. you don't use IIII but IV for example

1 Like

IIII = 4 is an addition rule.
IV = 4 is a subtraction rule.

How did the Roman arrive at that number system without the need of 0 (zero) -- intuitive or observation?

The use of "IV" instead of "IIII" in Roman numerals has several theories regarding its origin, but none of them has been fully confirmed - there were no strict rules for writing Roman numerals but IV became the norm (probably to avoid repeating the same symbol more than three times in a row which was longer than taking the subtractive principle).

it was primarily in India that the modern decimal numeral system with zero as a placeholder emerged. Roman were fine without the 0 :slight_smile: (The Roman numeral system is a non-positional system) as they used numbers to just count things. when there was nothing to count, then you just wouldn't need to count

2 Likes