hn.today

Bit Shifting in Basic

subethasoftware.com3 points0 comments
Screenshot of Bit Shifting in Basic

A project to generate shifted versions of an 8×8 graphic for the Commodore VIC-20 becomes a practical exploration of bit manipulation in Color BASIC. The author shows that, for byte-sized values, multiplying by two moves bits left and dividing by two moves them right. BASIC routines print each byte in binary, first checking bit positions with powers of two calculated repeatedly, then using a precomputed array to make the display faster. The examples shift a single set bit across a byte and explain why simply moving bits between two graphic bytes requires a wider intermediate value.

The solution combines two bytes into a 16-bit number, shifts that number, then separates it back into bytes. The account compares two splitting methods: division plus AND, and INT-based division followed by subtraction and multiplication; in the reported benchmark, the AND approach is faster. A complication is that Color BASIC’s AND function accepts values only up to 32767, so it cannot directly test the highest bit of an unsigned 16-bit value. The workaround prints a 16-bit value as two separate bytes. A final demonstration shifts &HFF through a 16-bit value and recovers the two changing bytes, producing the data needed for multiple character positions. The piece offers working routines and benchmarks while inviting suggestions for faster shifting, binary display, and byte conversion.

Read on subethasoftware.com0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.