Author 
Topic: Winter fun 2016 Binary Bits (Read 426 times) 

Rod
Administrator
member is offline
Graphics = Goosebumps!
Gender:
Posts: 3151


Winter fun 2016 Binary Bits
« Thread started on: Nov 1^{st}, 2016, 04:54am » 

This is a challenge that will refresh your thinking on how computers really work. How do they work? Bits of course. These Bits are combined into Bytes and larger memory structures.
So the task is to show how computer memory changes Bit by Bit when numbers and text are stored in a variable or manipulated using binary commands such as AND or OR.
You can use text or graphics to display how the Bits change.


Logged




bplus
Senior Member
member is offline
Gender:
Posts: 1255


Re: Winter fun 2016 Binary Bits
« Reply #1 on: Nov 10^{th}, 2016, 10:31am » 

Here is a bit of beauty!
Code:'binary expansion rationals.txt for Just Basic v1.01 [B+=MGA] 20161110
'much more developed for Rod's Challange since last version.
' graph 0's and 1's as black and white rectangles, to demonstate patterns
' of rationals like 1/15, 2/15, 3/15... 14/15 stacked on top of each.
' Notice how patterns shift, beautifully,
' as the numerator increases for a given divisor.
' Notice how the patterns for 1/3 and the patterns of 1/5
' are blended into and fit into pattern changes for 15,
' to me this is awesome!!! This is same for all factors
' of a divisor (if a composite number).
global H$, XMAX, YMAX, offs, width, height, goON, divisor
H$ = "gr"
XMAX = 1200 'Designed for 1200 but will adjust to narrower.
YMAX = 700 'Designed for 35 lines, so less here would cut off lines
'without an adjustment to height and font size as well.
nomainwin
WindowWidth = XMAX + 8
WindowHeight = YMAX + 32
UpperLeftX = (1200  XMAX) / 2 'or delete if XMAX is 1200 or above
UpperLeftY = (700  YMAX) / 2 'or delete if YMAX is 700 or above
open "Graphic of the Binary Expansion of Rationals, press spacebar to toggle screens" for graphics_nsb_nf as #gr
#gr "setfocus"
#gr "trapclose quit"
#gr "when leftButtonUp lButtonUp"
#gr "when characterInput charIn"
#gr "down"
#gr "fill blue"
'black and white rectangles will be drawn instead of 0's and 1's
offs = 80 'space for fraction in text
width = int( (XMAX  offs) /48 )  1 'x width
height = 20
divisor = 15
call divisorDisplay divisor
call numeratorDivisorLine 1, 3, 17
call numeratorDivisorLine 5, 15, 18
call numeratorDivisorLine 2, 3, 19
call numeratorDivisorLine 10, 15, 20
call numeratorDivisorLine 1, 5, 22
call numeratorDivisorLine 3, 15, 23
call numeratorDivisorLine 2, 5, 24
call numeratorDivisorLine 6, 15, 25
call numeratorDivisorLine 3, 5, 26
call numeratorDivisorLine 9, 15, 27
call numeratorDivisorLine 4, 5, 28
call numeratorDivisorLine 12, 15, 29
#gr "flush"
msg$ = "Equivalent fractions have equivalent patterns "
msg$ = msg$ + "even in binary white and black boxes "
msg$ = msg$ + "(representing bit 0 or 1)." + chr$(10) + chr$(10)
notice msg$ + "Press spacebar to toggle next screen..."
call wait4Spacebar
divisor = 16
while goON = 1
scan
call divisorDisplay divisor
if divisor mod 5 = 1 then
msg$ = "You may see first 34 lines of any integer divisor, "
msg$ = msg$ + "press d for prompt, "
notice msg$ + "otherwise press spacebar to cycle divisors 3 to 30."
end if
if divisor = 20 then
notice "Is it just my eyes or is there an optical illusion here?!?"
end if
divisor = divisor + 1
if divisor > 30 then divisor = 3
call wait4Spacebar
wend
wait
sub wait4Spacebar
goON = 0
while goON = 0
scan
wend
end sub
sub numeratorDivisorLine n, divisor, lineN
#gr "color white"
#gr "backcolor blue"
#gr "place ";10;" ";(lineN +.7)*height
#gr "";n;"/";divisor;" = "
#gr "color blue"
b = .5
r = n / divisor
flagstop = 0 : count = 0
while flagstop = 0
scan
if r < b then
#gr "backcolor white"
else
#gr "backcolor black"
if r = b then flagstop = 1
if r > b then r = r  b
end if
call fbox offs+count*width,lineN*height,offs+(count+1)*width,(lineN+1)*height
count = count + 1
'max character expansion = 48 in base 10 called decimal places
if count = 48 then flagstop = 1
b = b * .5
wend
end sub
sub divisorDisplay divisor
#gr "fill blue"
if divisor > 35 then forEND = 34 else forEND = divisor  1
for n = 1 to forEND
scan
call numeratorDivisorLine n, divisor, n
next
#gr "flush"
end sub
sub fbox x0, y0, x1, y1
#gr "place ";x0;" ";y0
#gr "boxfilled ";x1+1;" ";y1+1
end sub
sub lButtonUp H$, mx, my
call quit H$
end sub
sub charIn H$, c$
if asc(c$) = 32 then
goON = 1  goON
else
if c$ = "d" then
prompt "Enter your divisor.";test
if test > 2 then divisor = int(test)
else
call quit H$
end if
end if
end sub
sub quit H$
close #H$
end
end sub

« Last Edit: Nov 10^{th}, 2016, 10:40am by bplus » 
Logged

B+



