ShowBits64

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

ShowBits64

Post by dafhi »

ShowBits64

Code: Select all

Sub ShowBits32( p as any ptr, Newline As boolean = true)
  '' init gfx or u will see wrong color
  '' use ScreenRes x,y, 32
  Dim As ulong  I, AryFG(3), AryBG(3)
  var           oldcol = color()
  AryFG(0) = RGB(200,200,200)
  AryFG(1) = RGB(255,0,0)
  AryFG(2) = RGB(0,255,0)
  AryFG(3) = RGB(0,96,255)
  AryBG(0) = RGB(100,100,100)
  AryBG(1) = RGB(127,0,0)
  AryBG(2) = RGB(0,127,0)
  AryBG(3) = RGB(0,0,127)
  dim as ulong ptr  v = p
  For I = 0 To 3
    Color AryFG(I), AryBG(I)
    print bin(*v shr (24 - i*8), 8);
  Next
  If Newline Then Print
  color oldcol
End Sub
sub ShowBits64(p as any ptr, Newline As boolean = true)
  showbits32 p+4, false
  ShowBits32 p, newline
END SUB


type sng_x2
  as Single           a,b
END TYPE

type ulong_x2
  as ULong            a,b
END TYPE

union Union64
  as sng_x2           s
  as ulong_x2         u
  as double           d
  as ULongInt         uu
END UNION


screenres 800,600, 32

dim as Union64   n

for i as long = 0 to 23
  n.s.a = rnd
  n.u.b = i shl 8
  n.uu or= culngint(1) shl 34
  showbits64 @n
next

sleep
previous example: showbits32

Code: Select all

'
' Simple RNG by Thorham - macro adaptation by dafhi - 2014 Dec 28
' http://www.freebasic.net/forum/viewtopic.php?f=7&t=16044
'

Dim Shared As long t_rng_z, t_rng_d

#Macro tRNG()
  Asm
    mov ecx, [t_rng_z]
    mov eax, [t_rng_d]
    rol eax,7
    Add eax,ecx
    mov [t_rng_d],eax
    Add ecx, &H11111111
    mov [t_rng_z], ecx
  End Asm
#EndMacro

' ---- rng end ---- '


Sub ShowBits32( ByVal pAny32 As any ptr, ByVal NoNewline As Integer = 0)
   Dim As ulong strPos, bitPos, AryFG(3), AryBG(3),I,J
   dim as long ptr  p32 = pAny32
   Dim str_ As String * 8
   AryFG(0) = RGB(200,200,200)
   AryFG(1) = RGB(255,0,0)
   AryFG(2) = RGB(0,255,0)
   AryFG(3) = RGB(0,32,255)
   AryBG(0) = RGB(100,100,100)
   AryBG(1) = RGB(127,0,0)
   AryBG(2) = RGB(0,127,0)
   AryBG(3) = RGB(0,0,86)
   For I = 0 To 3
      Color AryFG(I), AryBG(I)
      For J = 0 To 7
         bitPos = (3 - I) Shl 3 + J
         strPos = 7 - J
         If bit(*p32, bitPos) Then str_[strPos] = 49 Else str_[strPos] = 48
      Next
      Print str_;
   Next  
   If Not NoNewline Then Print
End Sub


sub SeedRNG_and_ShowSomething(seed_a as long=0, seed_b as long=0)
  t_rng_z = seed_a
  t_rng_d = seed_b
  color rgb(200,200,200), rgb(0,0,0)
  ? "seeds "; t_rng_z; " "; t_rng_d
  ?
  for i as integer = 1 to 50
    tRNG()
    ShowBits32 @t_rng_z
  next: ?
end sub


Sub Main
  screenres 640, 480, 32
  randomize
  SeedRNG_and_ShowSomething Rnd * &HFffFFff, Rnd * &HFffFFff
end sub

Main

sleep
Last edited by dafhi on Jul 07, 2018 9:24, edited 14 times in total.
mjs
Site Admin
Posts: 842
Joined: Jun 16, 2005 19:50
Location: Germany

Post by mjs »

FWIW:

Code: Select all

sub ShowBits( byval value as integer )
    dim s as string
    s = right$(bin$(&H100000000 + value),32)
    color 0,11
    print mid$(s,1,8);
    color 2,10
    print mid$(s,9,8);
    color 13,9
    print mid$(s,17,8);
    color 7,5
    print mid$(s,25,8)
end sub
How abount an optimization contest? *g*

Regards,
Mark
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Post by yetifoot »

is smaller or faster code the target?
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

It is nice to see a simple example of ASM though.
Is there a way of getting the original COLOR values back?
mjs
Site Admin
Posts: 842
Joined: Jun 16, 2005 19:50
Location: Germany

Post by mjs »

counting_pine wrote:Is there a way of getting the original COLOR values back?
Try using the SCREEN function.

Example:

Code: Select all

char_value = SCREEN(row, col)
color_value = SCREEN(row, col, 1)

fg = color_value and &H0F
bg = color_value shr 4
EDIT: Ooops, I realized that you probably meant the following:

Code: Select all

sub ShowBits( byval value as integer )
    dim old_color as integer
    dim s as string
    old_color = color() ' remember old color values
    s = right$(bin$(&H100000000 + value),32)
    color 0,11
    print mid$(s,1,8);
    color 2,10
    print mid$(s,9,8);
    color 13,9
    print mid$(s,17,8);
    color 7,5
    print mid$(s,25,8)
    color (old_color and 15), (old_color shr 16) ' restore old color values
end sub
Regards,
Mark
Sisophon2001
Posts: 1706
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Post by Sisophon2001 »

Hi:

In my tests Marks code is 6 times slower than the assembly, but by cutting down on the print calls, I could make it 20% faster than the assembly (timed with 100 loops, redirected to file).

Regards

Garvan

Code: Select all

sub ShowBits3( byval pInput as integer )
	dim i
    dim s as string * 8
	color 0,11
	for i = 0 to 7
		if bit(pInput, 31-i) then s[i] = 49 else s[i] = 48
	next
    print s;
	color 2,10
	for i = 0 to 7
		if bit(pInput, 23-i) then s[i] = 49 else s[i] = 48
	next
    print s;
	color 13,9
	for i = 0 to 7
		if bit(pInput, 15-i) then s[i] = 49 else s[i] = 48
	next
    print s;
	color 7,5
	for i = 0 to 7
		if bit(pInput, 7-i) then s[i] = 49 else s[i] = 48
	next
    print s
end sub
EDIT: Corrected bug. Lets hope it works this time!

Edited again:

This looks like one of those posts where I got everything mixed up.

One more try:

mjs code is six times faster than the assembly code because of the time savings on the print statements. The version using the bit function is still fastest by about 20%.

Garvan
Last edited by Sisophon2001 on Nov 01, 2005 2:08, edited 2 times in total.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

I wouldn't know how, but I suspect the ASM version could be optimised as well.
You could at least use J = I \ 8 or J = I Shr 3 instead of J = Int(I / 8). It's almost surreal to see a statement like that in the middle of some assembly code.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Post by dafhi »

asm (updated) was the only way i knew how to do this problem.
also made this one based on counting_pine and Sisophon's input.
for my own personal use i've kept the nice short one by mjs.

thanks guys.

Code: Select all

Sub ShowBits_FAST( byval value as integer )
    dim as uinteger I, J, strPos, bitPos, AryFG(3), AryBG(3) 'unsigned 32 bit
    dim sav_color = Color(), s as string * 8
    
    AryFG(0) = 0
    AryFG(1) = 2
    AryFG(2) = 13
    AryFG(3) = 7
    AryBG(0) = 11
    AryBG(1) = 10
    AryBG(2) = 9
    AryBG(3) = 5
    
    for I = 0 to 3
        Color AryFG(I), AryBG(I)
        for J = 0 To 7
            bitPos = (3 - I) shl 3 + J 'first position = 0
            strPos = 7 - J           'first position = 0
            if bit(value, bitPos) then s[strPos] = 49 else s[strPos] = 48
        next
        Print s;
    Next    

    print
    color sav_color
    
end sub
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

You can also look here to see how I did "bit arrays": http://www.freebasic.net/forum/viewtopic.php?t=1699
Post Reply