Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Is there a bit-shift function?

  1. #1

    Is there a bit-shift function?

    Is there a bit-shift function?

    I use this on a few places, due to speed, and simplicity of code. I was just wondering if there was a function similar to this here?

    In JAVASRIPT, it is done like this...
    http://developer.mozilla.org/en/Core...wise_Operators

    The ones I use a lot are...
    a >> b
    a << b

    I also use a lot of 0x8

    Is that a valid bit numerical representation? (Still sifting through the help, looking for bitwise operations. Ones that work on all values, not just numbers.)

  2. #2
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: Is there a bit-shift function?

    [code=thinbasic]' Usage of the SHIFT Instruction example

    ' Binary Divide by 2 (no remainder)



    Dim MyByte As Byte VALUE &h08 ' Initialise Byte to 8

    Dim sMsg As String



    sMsg += "Original Byte Value " & Hex$(MyByte,2) & $CRLF & $CRLF



    SHIFT SIGNED RIGHT MyByte,1 ' Binary Divide by 2 probably quicker than a /



    sMsg += " New Byte Value " & Hex$(MyByte,2) & $CRLF



    MSGBOX 0, sMsg[/code]

    Its all in the help file.
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,782
    Rep Power
    10

    Re: Is there a bit-shift function?

    Yes, is present.
    Called SHIFT. More info at http://www.thinbasic.com/public/prod...html/shift.htm
    Can be used only on integer class numeric values: Byte, Word, Integer, DWord, Long, Quad
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4

    Re: Is there a bit-shift function?

    I can work with that... I just have to convert the string to a byte array. Thanks.

    (Mostly I use it for making custom GIF's. Many people don't realize that each cell can have its own separate color pallet. The cheap GIF programs only allow one pallet, which is used for the whole animation.)

    But it also comes in handy for ghetto encryption. (Easy to hack, but most assume that garbled text/data is actually encrypted, not just bit-shifted.)

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,782
    Rep Power
    10

    Re: Is there a bit-shift function?

    Quote Originally Posted by ISAWHIM
    I can work with that... I just have to convert the string to a byte array. Thanks.
    Maybe you do not have to convert the string into a byte array but just over impose a byte array to the string.
    thinBasic has a nice very powerful DIM ... AT functionality that let you handle the same sequence of memory in different ways.

    Example
    [code=thinbasic]
    USES "Console"

    '---Define a string
    dim MyString as string

    '---Fill it with something
    MyString = "Hi there."

    '---Now define a byte array of the same lenght of the string
    '---and with the AT option tells thinBasic to not allocate any
    '---new memory but use the same memory area of what is located
    '---at the same memory pointer of the MyString variable
    DIM MyByteArray(len(MyString)) as byte at strptr(MyString)

    '---
    dim Counter as long
    for Counter = 1 to ubound(MyByteArray)
    printl Counter, MyByteArray(Counter), chr$(MyByteArray(Counter))
    next
    waitkey
    [/code]

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Re: Is there a bit-shift function?

    EDIT: Eros was faster

    Hi ISAWHIM,

    if you mean to get binary encoded value in string buffer to number, there are multiple ways.

    Way #1 - conversion
    To convert raw binary data to correct datatype, you can check out CVx family of functions.
    For example, if you have 4 characters long ASCII string, you can convert it to byte values using

    [code=thinbasic]CVBYT( <stringHere>, <nthValue> ).[/code]

    This function takes string, and converts the value.

    Way #2 - memory overlay
    While conversion is ok, we always need the best speed. And what is fastest? Not doing any conversion at all, just play with the way to look at memory data.

    ThinBasic allows to overlay variable over other variable, even of different type.
    So lets think we have again 4 character long string, encoding 4 byte values.

    [code=thinbasic]
    ' -- Presume strData is that 4 character string

    DIM MyByte(4) AS BYTE AT STRPTR(strData)
    [/code]

    This will create BYTE array over memory location occupied by string. Once this line is executed, the array immediately (!) contains all values as BYTE numbers ( 0 - 255 ).

    This works even to other side - if you assign values to this array element, it will update the string.

    Here is little example:
    [code=thinbasic]
    Uses "Console"

    DIM i AS LONG

    ' -- This string will contain binary encoded 4 BYTE values, just for testing
    ' -- The string could contain raw data from image file for example
    dim strData as string = mkBYT$(1, 10, 20, 40)

    PRINTL "Conversion ->"
    FOR i = 1 to 4
    PRINT CVBYT(strData, i)+ " "
    NEXT

    PRINTL
    PRINTL

    PRINTL "Direct overlay ->"

    DIM MyByte(4) AS BYTE AT STRPTR(strData)

    PRINTL "Printing iteratively:"
    FOR i = 1 to 4
    PRINT MyByte(i)+ " "
    NEXT

    PRINTL
    PRINTL

    PRINTL "Printing FAST:"
    ' -- One line of code, all array elements printed to screen
    PRINTL JOIN$(MyByte, " ")

    waitkey
    [/code]


    Bye,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  7. #7
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,782
    Rep Power
    10

    Re: Is there a bit-shift function?

    I was faster, You were more professional.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  8. #8

    Re: Is there a bit-shift function?

    Yea, but petr made my brain melt more...

    I was thinking about using the TYPE function to set the string into a byte and string at the same time. That way I could update the byte with the new shifted value, and pull out the string on the fly. (Otherwise I would have to convert and join it back to a string. As I originally had done with small values. Not talking about your samples.)

    I don't have any fancy code yet... I am still in brain-melted state from the above examples. (I was thinking about this again, since I started playing with the ZIP function, as a ghetto-protection of the compressed data. I already reverse the whole file before I zip it... and get the SHA1 FWD and REV, so I have 100% pure thumbprint of the file. There is no physical/code way to alter any piece of the file, and not change the SHA1 if it is read both ways. Even a normal CRC32 becomes unhackable from injection alteration. But anywho... Back to the topic.)

    The only issue I have to overcome, is that I can not loose the shifted bit value... (Normally, a simple compare would return the value of the bit that would get lost, so it could be reset back on the other side. I know there are many ways to do that also.)

    EG...

    01110111 << 2 = 11011100
    The first two {01} get dropped, and the last two get replaced with {00}
    (But I need to put the dropped {01} back onto the other side.)

    The easy way, is to just double the value...
    (8bit) & (8bit) as (16bit)
    01110111 & 01110111 = (0111011101110111)
    Now shift
    0111011101110111 << 2 = 1101110111011100
    Now take the first value (8 bits) which had the left {01} pushed off, but the {01} from the following (8bits), was pushed into the value, to cause a full looped shift. (Reversing it using >> would make you want the second value, when you are attempting to restore it.)

    Thus, if done in reverse... the original value is restored.
    As opposed to...

    11111111 << 2 = 11111100
    than
    11111100 >> 2 = 00111111
    result
    00111111 != 11111111

    I know there is a machine version of this. (No clue about machine language... no samples please... You'll kill me!)

  9. #9
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Re: Is there a bit-shift function?

    Hmm,

    I think SHIFT function works correct. The result you want I would call "rotate bits" rather than "shift bits".
    I think similar terminology is used in assembly.

    Well, now the time has come, Charles will shoot me if I did something wrong but I hope it is correct and I learned something from him :
    [code=thinbasic]
    uses "Console"
    Uses "Oxygen" ' -- Here is the best assembler ever

    %myAssemblerRotLeft = 1
    %myAssemblerRotRight = 2

    dim i as long
    dim b as byte

    ' -- Assembler source codes to rotate left
    dim srcLeft as string = "
    mov al,[#b]
    rol al, 1
    mov [#b],al
    ret
    "

    ' -- Assembler source codes to rotate right
    dim srcRight as string = "
    mov al,[#b]
    ror al, 1
    mov [#b],al
    ret
    "

    ' -- JIT compile assembler to native code
    O2_Buf %myAssemblerRotLeft
    o2_asmo srcLeft

    O2_Buf %myAssemblerRotRight
    o2_asmo srcRight

    PRINTL "-= Testing rotate left =-"
    b = 119
    O2_Buf %myAssemblerRotLeft
    for i = 1 to 8
    printL bin$(O2_exec, + $SPC(2) + bin$(b, + $SPC + FORMAT$(b, "000")
    next

    printl
    PRINTL "-= Testing rotate right =-"
    b = 119
    O2_Buf %myAssemblerRotRight
    for i = 1 to 8
    printL bin$(O2_exec, + $SPC(2) + bin$(b, + $SPC + FORMAT$(b, "000")
    next

    waitkey
    [/code]

    If you need this functionality, I would suggest you to make suggestion in Suggest new feature ( suggest3 ).
    The code above can be used meanwhile, seems to work.
    ( mov = put value to register, rol = rotate left, ror = rotate right )


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  10. #10

    Re: Is there a bit-shift function?

    Petr got there before me.

    This example shows 32 bit rotation - rotating to the right by 4 places
    If you want it to do this on a single byte then it is

    ror byte [#vv],4

    there is also
    shl - shift left
    shr - shift right
    sar - shift arithmetic right (preserving sign)
    and
    rol - rotate left
    rcr rotate through carry right
    rcl rotate through carry left

    [code=thinbasic]

    uses "OXYGEN"

    dim vv as long
    dim src as string

    src="
    ror [#vv],4
    ret
    "
    'END OF SRC

    o2_asmo src
    if len(o2_error) then
    msgbox 0,o2_error()+o2_view (src)
    stop
    end if

    vv=2

    o2_exec
    msgbox 0,"0x"+hex$(vv)

    [/code]

Page 1 of 2 12 LastLast

Similar Threads

  1. For SHIFT keyword
    By ErosOlmi in forum Samples for help file
    Replies: 0
    Last Post: 26-05-2007, 10:25

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •