Flipping the bits.

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Flipping the bits.

Post by Shaun_B » Sat Oct 18, 2014 11:08 pm

Hi all,

I was doing some VIC-20 programming earlier this week and someone suggested that I should add a custom character set to my program. As I'm lazy, I did a quick char set basically reducing the size of most characters by one pixel wide and high, but then I realised that I needed a reverse set of the same characters.

I could have used the char editor and done the reverse set but then I realised that this would require loading in twice the about of data and as I was likely to be doing a tape load (as this could be a Cronosoft release), this didn't seem like a good idea. So, here's a way to 'flip the bits' in CBM BASIC (in assembly, you'd simply XOR %11111111 or something like this).

Code: Select all

0 print "{clear}";
10 print "bit pattern 10101010 is 170 in decimal"
20 print "bit pattern 01010101 is 85 in decimal"
30 let x = 170
40 let y = not x and 255
50 print x; "flipped is"; y
60 print y; "flipped back again is"; not y and 255
Now, if CBM BASIC had C style << and >> operators, this would be very useful. Sadly not though, unless anyone knows of a way to achieve this? ( like x = 1; y = x << 3; )

Regards,

Shaun.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.

User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Flipping the bits.

Post by Shaun_B » Sun Oct 19, 2014 11:26 am

Here's a program listing that demonstrates roughly how the C bitwise operators << and >> work:

Code: Select all

0 print "{clear}";:gosub 1000
10 input "enter number to shift";x
30 input "how many bits to move";y
40 input "< to shift left or > to shift right";a$: print
50 print "{left}";x;"in binary is: ";
60 let b=x: gosub 500
70 on -(a$="<")-t*(a$=">") gosub 100, 200
80 print "{left}";x;"in binary is: ";: gosub 500
90 end
100 rem x << y
110 for i=1 to y
120 let x=x+x
130 next i
140 print "{left}";b;"<<";y;"is";x
150 return
200 rem x >> y
210 for i=1 to y
220 let x=x/t
230 next i:let x=x and x
240 print "{left}";b;">>";y;"is";x
250 return
500 rem convert number to binary:
510 let n=128
520 for i=z to s
530 print b$(sgn(x and n));
540 let n=n/t
550 next i: print: return
1000 rem variables and constants:
1010 dim b$(1), a$, b, x, y, n, z, t, s, i
1020 let b$(0)="0": let b$(1)="1"
1030 let z=0: let t=2
1040 let s=7: let n=128
1050 return
There's also a routine in there that'll convert any 8-bit number (0 to 255) to its binary equivalent.

Because we're worrying about the bits and not necessarily the numeric values, 20 >> 4 will produce the same result as 19 >> 4, but 1 << 4 (the answer to 20 >> 4) will not produce 19 or 20.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.

Post Reply Previous topicNext topic