How to "reverse" a byte?

New to FreeBASIC? Post your questions here.
grindstone
Posts: 752
Joined: May 05, 2015 5:35
Location: Germany

Re: How to "reverse" a byte?

Postby grindstone » Jan 18, 2017 13:46

I like to misappropriate strings as byte arrays because
    1) FB takes care of the memory management and
    2) I don't know a faster and easier way to access a single byte than an indexed string access
MrSwiss
Posts: 3610
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to "reverse" a byte?

Postby MrSwiss » Jan 18, 2017 14:06

grindstone wrote:I like to misappropriate strings as byte arrays ...
Well, behind the scenes, its exactly the same as a Byte-Array access.
So, if a Array-Size is fixed (as in above case), you're just creating unneeded overhead (String-Header).
fxm
Posts: 9947
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to "reverse" a byte?

Postby fxm » Jan 18, 2017 14:32

One can also work with a fix-len string:

Code: Select all

Dim Shared As String * 256 table
instead of:

Code: Select all

Dim Shared As String table
table = String(256,Chr(0)) 'allocate the memory and fill with "0"s
fxm
Posts: 9947
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to "reverse" a byte?

Postby fxm » Jan 18, 2017 14:34

MrSwiss wrote:
grindstone wrote:I like to misappropriate strings as byte arrays ...
Well, behind the scenes, its exactly the same as a Byte-Array access.

Execution time is equivalent if one does not compile with option -exx.
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: How to "reverse" a byte?

Postby vdecampo » Jan 18, 2017 17:11

Maybe I missed something but can't you just do this?

Code: Select all

Dim value as long= &hFFFF0000
dim rev as long = value XOR &hFFFFFFFF
print hex(value,8), hex(rev,8)
sleep


-Vicne
I3I2UI/I0
Posts: 90
Joined: Jun 03, 2005 10:39
Location: Germany

Re: How to "reverse" a byte?

Postby I3I2UI/I0 » Jan 18, 2017 17:49

Hi Luis,
a fast way:

Code: Select all

'bitweise gespiegelt (abcde... ->  ...edcba)
#Macro mirror8(Variable)
Asm
  mov al, [Variable]
  mov bl, 0
  mov cl, 8
  1:
  rcr al
  rcl bl
  dec cl
  jnz 1b
  mov [Variable], bl
End Asm
#EndMacro

Dim As UByte zahl = &b00111011
Print Bin(zahl, 8), zahl
mirror8(zahl)
Print Bin(zahl, 8), zahl
mirror8(zahl)
Print Bin(zahl, 8), zahl
Sleep
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

Re: How to "reverse" a byte?

Postby Luis Babboni » Jan 18, 2017 19:26

Sorry guys, I do ot understand so much your discussion.

I tried the suggestion from MrSwiss (the last post of page 1) and works nice..... but not know how to see how fast it really is.
This about I3I2UI/I0 is faster?

VDcampo, I think this is not what I want, I want to "flip" the byte horizontally to say it in other words.
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

Re: How to "reverse" a byte?

Postby Luis Babboni » Jan 18, 2017 19:29

fxm wrote:
MrSwiss wrote:
grindstone wrote:I like to misappropriate strings as byte arrays ...
Well, behind the scenes, its exactly the same as a Byte-Array access.

Execution time is equivalent if one does not compile with option -exx.


I´m compiling my program with "Windows console " option saying below "fbc -s console"
Not know too much what all this meaning. :-(
fxm
Posts: 9947
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to "reverse" a byte?

Postby fxm » Jan 18, 2017 19:47

MrSwiss
Posts: 3610
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to "reverse" a byte?

Postby MrSwiss » Jan 18, 2017 19:48

@Luis,

assembly language may be faster but, on the other hand, less compatible:
you'd have to do separate ASM routines for FBC 32bit and FBC 64bit.

With routines written in FB, you are able to use any of the compilers.
You'll have to decide on which option to choose.
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

Re: How to "reverse" a byte?

Postby Luis Babboni » Jan 18, 2017 19:55

Mmmmm, may be is not enough with this invByte for 8 bits bytes... :-(

I´m afraid I need more help to understand if the following, that seems to be made in c++, is possible to be doing in FB.
Note: all is from here: https://chessprogramming.wikispaces.com ... ce+Attacks
(The idea behind is to represent a chessboard (8x8 grid) as a 64 bits byte with 1s for occuped squares and 0s for empty squares).

First thing (I solved):
in positive ray direction:
occupied 11000101
slider 00000100 subset of occupied
o-s 11000001 resets the slider in occupied
o-2s 10111101 borrows the one from nearest blocker
occupied 11000101
o^(o-2s) 01111000 all flipped bits are the attacks in most significant bit direction


Second thing (you solved for me):
For negative directions:
with o' = reverse (o)
and s' = reverse (s)
negativeRayAttacks = reverse (o' ^ (o' - 2s'));


But for the next I´m not sure if it is possible to solve it in FB (or at least for sure not by me!!):
Diagonal or vertical line occupancies need leading and trailing intersection with line-masks. Thanks to the little-endian versus big-endian war, recent processors have appropriate instructions to swap bytes (ranks) inside a 64-bit word for the reverse arithmetic, note that masked files or diagonals have only max one bit per rank:
U64 diagonalAttacks(U64 occ, int sqOfSlider) {
U64 forward, reverse, slider, lineMask;
lineMask = diagonalMaskEx[sqOfSlider]; // excludes square of slider
slider = singleBitboard[sqOfSlider]; // single bit 1 << sq, 2^sq
forward = occ & lineMask; // also performs the first subtraction by clearing the s in o
reverse = byteswap( forward ); // o'-s'
forward -= ( slider ); // o -2s
reverse -= byteswap( slider ); // o'-2s'
forward ^= byteswap( reverse );
return forward & lineMask; // mask the line again
}
Last edited by Luis Babboni on Jan 18, 2017 20:01, edited 1 time in total.
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

Re: How to "reverse" a byte?

Postby Luis Babboni » Jan 18, 2017 19:59

I´m wrong or it saids that it is needed to do the same than you did with invByte for 8 bits byte now with 64 bits byte??!! :-(
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

Re: How to "reverse" a byte?

Postby Luis Babboni » Jan 18, 2017 20:00

MrSwiss wrote:@Luis,

assembly language may be faster but, on the other hand, less compatible:
you'd have to do separate ASM routines for FBC 32bit and FBC 64bit.

With routines written in FB, you are able to use any of the compilers.
You'll have to decide on which option to choose.


Just now I saw his suggestion is in assembler! I´ll kept in FB for the moment :-D
Thanks!
MrSwiss
Posts: 3610
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How to "reverse" a byte?

Postby MrSwiss » Jan 18, 2017 20:04

Just coded an Endian-Inverter for 64bit INT ...
64bit conversion Big-/Little- Endian (here)
This is however, a Byte by Byte conversion (not bit by bit).
Luis Babboni
Posts: 303
Joined: Mar 15, 2015 12:41

Re: How to "reverse" a byte?

Postby Luis Babboni » Jan 18, 2017 20:32

MrSwiss wrote:Just coded an Endian-Inverter for 64bit INT ...
64bit conversion Big-/Little- Endian (here)
This is however, a Byte by Byte conversion (not bit by bit).


Mmmm, I need to combine this with invByte to do it bit by bit?

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest