Page 1 of 1

order of binary digits

Posted: Fri May 20, 2022 9:15 pm
by jeffythedragonslayer
When I see binary digits written out in 65x code, is the order #%01234567 or #%76543210? I know there isn't really a concept of bit-endianness but is the most or lead significant bit written first?

Re: order of binary digits

Posted: Fri May 20, 2022 9:50 pm
by gfoot
jeffythedragonslayer wrote:
When I see binary digits written out in 65x code, is the order #%01234567 or #%76543210? I know there isn't really a concept of bit-endianness but is the most or lead significant bit written first?
Usually the most significant digit is written first, like in other number bases.

Re: order of binary digits

Posted: Fri May 20, 2022 9:57 pm
by Dr Jefyll
gfoot wrote:
Usually the most significant digit is written first, like in other number bases.
Yes. And the numbering of those bits is usually from right to left. That is to say, the least-significant bit (the one on the far right) is Bit 0.

-- Jeff

Re: order of binary digits

Posted: Fri May 20, 2022 11:47 pm
by jeffythedragonslayer
Ok thanks, so usually #%76543210. When might this "usually" rule of thumb break down and throw me off?

Re: order of binary digits

Posted: Sat May 21, 2022 12:25 am
by GARTHWILSON
There are no exceptions to this rule. Ever. What you might be thinking of is byte order (as opposed to bit order). In 16-bit quantities, 6502 puts the low byte first. This makes it more efficient in certain circumstances. See the explanation and examples at the bottom of the 6502 primer page about memory-map requirements, at http://wilsonminesco.com/6502primer/MemMapReqs.html, specifically the last five paragraphs, after the heading "Low Byte First."

Also, UARTs output bit 0 (the lsb) first. They go backwards. Bit 0 is still the lsb though. (In terminology, "lsb" in lower-case is "least significant bit," and "LSB" in capitals is "least significant byte." The same goes for msb and MSB for most significant bit and byte. Yes, case matters.)

Re: order of binary digits

Posted: Sat May 21, 2022 5:41 am
by noneya
As a visual example, assume you have a 6522 VIA, you have set your data direction register for output and you want to follow that up by setting pin 2 (bit 1) to a 1 (hot), and all others to zero.

To accomplish this you would write hex #$02 or %00000010 to the given via port. Looking at the pins of this via port from left to right, we would observe the following relationship whereas X indicates the pin is hot ( a one) and others are "off" ( a zero) :
hex #$02 or
%0 0 0 0 0 0 1 0 will yield a pin relationship of
_ X _ _ _ _ _ _

The reason for this is that the bits are shifted into the via. i.e. shift registers.
> %0 1 0 0 0 0 0 0 << Representation after being shifted into the VIA
^ _ X _ _ _ _ _ _
< %0 0 0 0 0 0 1 0 << Value as you would code

Re: order of binary digits

Posted: Sat May 21, 2022 5:50 am
by BigDumbDinosaur
jeffythedragonslayer wrote:
Ok thanks, so usually #%76543210. When might this "usually" rule of thumb break down and throw me off?

As Garth said, never. Bit order is an industry standard, not just confined to the 65xx world. In the case of the 65C816 running in native mode with 16-bit registers, the bit order still holds; bit 0 is on the right and bit 15 is on the left.

Also, #%76543210 would not be a valid operand. Only 0 and 1 are allowed in a bit field.

Re: order of binary digits

Posted: Sat May 21, 2022 8:42 am
by John West
I don't remember ever seeing bits written with the least significant on the left and the most significant on the right, but I have seen them numbered in the opposite order. That is, the bits in an eight bit value would be numbered 01234567, where bit 7 is the least significant (the 2^0 = 1 place) and bit 0 is the most significant (2^7 = 128).

This was on PowerPC, which I believe inherited it from POWER. It was as confusing as you might expect.

Re: order of binary digits

Posted: Sat May 21, 2022 11:18 am
by BigEd
I have a feeling TI did the same, in the 9900 era. Yep, datasheeet confirms it!

Re: order of binary digits

Posted: Sat May 21, 2022 12:28 pm
by BillG
The IBM 360 is the earliest computer I know in which bit #0 is the most significant.

Except for some bit test, set and reset instructions, it is not important to know which one is considered to be bit #0.

Re: order of binary digits

Posted: Sat May 21, 2022 3:35 pm
by barrym95838
Capture2.PNG

Re: order of binary digits

Posted: Tue Jun 14, 2022 12:19 pm
by Sheep64
GARTHWILSON on Sat 21 May 2022 wrote:
There are no exceptions to this rule. Ever.
You say that as competent professional but I have a lightly contrived scenario where a newbie may go astray.

Step 1: Get 6502 executing NOPs on a breadboard. We'll ignore that they should be using 65816 and avoiding breadboards but we'll keep going.

Step 2: Wire 6502 to ROM. Get 6502 to idle loop at $FD00 and $FE00 and use address line 8 to output Morse code. This requires wiring address lines and data lines in a sane order.

Step 3: Wire ROM to RAM. This works when the address lines and data lines are in any order - intentionally or not.

Step 4: Wire RAM to 6522. This is where the fun begins. Any drafts-person will know to measure from one spot. The same principle should apply when extending a bus. Don't make the address lines or data lines match the RAM. Make them match the processor.

Step 5: Attempt to fix hardware bug in software.

Re: order of binary digits

Posted: Tue Jun 14, 2022 5:29 pm
by commodorejohn
Yeah, some computer manufacturers numbered bits with #0 as the MSB, for...reasons. Don't know what the rationale may have been, unless it was just that, well, you write the MSB first, but it's always driven me crazy as it fails the simple logical test that place value = 2 ^ bit #. TI is the only place I know of that continued it into the relative modern day, although they seem to have finally abandoned it going by the MSP430 documentation.