Results 1 to 11 of 11

Thread: Can someone tell me what this does?

  1. #1
    Guest

    Question

    I need to know what this little thing does, like by line.

    int i = 0;
    unsigned int id = 0;
    int l = name.length();
    while (i < l)
    {
    unsigned int a = 0;
    for (int j = 0; j < 4; j++)
    {
    a >>= 8;
    if (i < l)
    a += static_cast<unsigned int>(name[i]) << 24;
    i++;
    }
    id = (id << 1 | id >> 31) + a;
    }
    return id;
    }

    Atleast I want to know what >> and << means, static_cast line doesn't tell to me nothing.

    I'm translating this to Visual Basic, but no code needed, only what it does. Thanks already.

  2. #2

    Cool

    Well, the code starts with i and id variables to be of type int and unsigned int respectively; it sets their initial values to 0. Variable l is set to the value returned by length method of name object variable. Then a loop starts through [l]while[/l], this loop keeps running as long as i is not equal to l.
    Inside this loop:
    * Another variable is declared and initialized in the same way mentioned above.
    * The bits of a variable are shifted to the right by 8. This is the meaning of >> operator.
    * static_cast<unsigned int>(name) is unclear for me!!
    * i is increased. This is done through ++ operator.
    * I think you understand this formula: id = (id << 1 | id >> 31) + a;.

    Upon the loop terminal, the value of id is returned through the keyword return. However, this only works if this code is a part of a function!
    Note: there's an extra bracket "}" which seems to be put by mistake or there is a starting bracket "{" before the start of the code but you did not place it here.

    Hope this helps ...

    Wesam

  3. #3
    Guest
    The lines I'm stuck with are:

    a >>= 8;
    static_cast<unsigned int>(name) << 24;
    id = (id << 1 | id >> 31) + a;


    So when a>>=8 occurs, is it same as a*256 or something?

    static_cast<unsigned int> doesn't tell me anything at all, except I do know what unsigned int is.

    << 24 Bugs me a lot, if I've understood << and >> right (and I'm sure I haven't).

    I know about that last line that in VB it could be id = (id?1? or id?31?) + a or something like that. Those are the last lines are unsolved.


    The code is used to calculate file ID for one game. It recognizes files by ID, not by filename and I need to calculate the ID so I can modify the game.

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Maybe this will help you a bit.Here is what MSDN says about static_cast.
    static_cast Operator
    The expression static_cast < type-id > ( expression ) converts expression to the type of type-id based solely on the types present in the expression. No run-time type check is made to ensure the safety of the conversion.

    Syntax

    static_cast < type-id > ( expression )

    The static_cast operator can be used for operations such as converting a pointer to a base class to a pointer to a derived class. Such conversions are not always safe. For example:

    class B { ... };

    class D : public B { ... };

    void f(B* pb, D* pd)
    {
    D* pd2 = static_cast<D*>(pb); // not safe, pb may
    // point to just B

    B* pb2 = static_cast<B*>(pd); // safe conversion
    ...
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5

    Cool

    1) a >>= 8;:
    This statement sets the value of a to be the its
    old value shifted to the right by 8; I am not sure if
    you know what bits shifting means?!
    2) static_cast<unsigned int>(name) << 24;
    This statement has almost the same meaning of the
    aforementioned statement, some value contained in name
    is converted to unsigned int through the
    casting method as Vlatko mentioned, then this
    value is again sifted to the left 24 bits.
    3) id = (id << 1 | id >> 31) + a;
    This statement contains several statements in one, gets
    the values returned by shifting the value of id
    to the left by 1, then getting the same value but by
    shifting id to the right by 31, then bitwise OR
    is performed on both values (theoretically, setting the
    zero bits to 1 as possible); then the resultant value is
    added to the value of a variable.

    Well, this is a literal interpretation, I hope it could help.

    Wesam

  6. #6
    Guest

    Bit shifting

    Bit shifting...Is it like

    (one to left)

    00 00 00 01 -> 00 00 00 10

    or something else?

    And how it works to right? Does the values swap like:

    00 00 00 01 -> 10 00 00 00

    I don't know...


    I found one article that said value<<2 is same as value*4 but it's faster.

    In that way value<<24 would be value*16777216...

    (I use value* things because it's like that in VB)


    Now I would like to have documents about C/C++...All I've learned I've read at sourcecode (doing a game with DJGPP sometimes - http://undergrn.cjb.net - I've only one document that helps mainly with things needed in game coding)

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    bitwise shifts are like in your first example:

    so 2 << 2 is like this:

    before:

    00000010

    after:

    00001000

    geddit?

    It will bring in 0's from the right when you shift left and just dump whatever gets shifted off the left end. With shifting right it's a bit different because of the way negative numbers are represented in the two's complement binary representation. Negative numbers start with the bit 1. When you do a bitwise shift to the right, the new bits that are carried in are the same as the sign bit, so:

    10110010 shifted right 2 becomes 11101100
    but
    00110010 shifted right 2 becomes 00001100


    I hope that clears it up a little, if you need some more explanation I'd be happy to try to help further
    Harry.

    "From one thing, know ten thousand things."

  8. #8
    Guest
    Hmm...So I can represent that in VB by * and / operators...Somehow. I've to think how I remove too big values. I don't know if there's still any functions I don't know.

    Well, I continue trying to solve the problem. I'm only now having problems with << and >>.

    Maybe I should change my values to binary, make the shifting and so on...But that will be slow.


    Thanks for the help guys

  9. #9
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    How important is speed for this? The reason shifts are use is usually for speed. You can replicate shifts in VB with * and / by 2^shiftValue as you said, but you don't get the speed benefit.

    If you need help figuring out how to accomodate overflows, just say and I'll try to figure it out. I'm a bit knackered @tm, been up since 2:30 am and it's nearly 8 pm, so I'm not really in the right state of mind to be thinking about maths and binary If you give it some thought I expect you'll be able to work it out.
    Harry.

    "From one thing, know ten thousand things."

  10. #10
    Guest
    It really doesn't need to be high speeded. It just makes those IDs at filenames. Filename is first converted to upper case, then just made to that kind of ID.

    Examples:

    ADDONS.MIX should return 2659769296 in desimals, 9E88DBD0 hex.

    CUSTOM.MIX - 2829514709 - A8A6F7D5


    I don't know if this helps...Anyway I did somekind of routine that should be correct but it makes ID too small.

  11. #11
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    It sounds like you're having trouble converting this line:

    id = (id << 1 | id >> 31) + a;

    Correct me if I'm wrong.

    I have to admit I don't know if you can use unsigned integers in VB. Also there are (as far as I know) no bitwise operators in VB so the bitwise OR (the | character) can't be translated directly.

    If speed isn't a problem, I am going to suggest that you create a string of 1s and 0s representing your value and create your own bitwise OR function to use on it. This may not be the best way to do it of course, but I'm sitting here in between lectures and I'm not thinking too hard about it Basically I'd make a bitwise shift function and a bitwise or function and have them both take integer arguments, but internally they convert to strings and back again for the purposes of working it out.

    Anyway here's some (unchecked) code for you to try:

    Code:
    Public Function bitOr(arg1 As Integer, arg2 As Integer) As Integer
    Dim x As String
    Dim y As String
    Dim resultStr As String
    x = intToBinStr(arg1)
    y = intToBinStr(arg2)
    resultStr = ""
    For i = 1 To Len(x)
        If Mid(x, i) = 1 Or Mid(y, i) = 1 Then
            resultStr = resultStr & "1"
        Else
            resultStr = resultStr & "0"
        End If
    Next i
    bitOr = binStrToInt(resultStr)
    End Function
    
    Public Function bitShift(arg As Integer, shiftToRight As Integer, shiftBy As Byte) As Integer
    Dim x As String
    Dim resultStr As String
    resultStr = ""
    x = intToBinStr(arg)
    For i = 1 To Len(x)
        If (shiftToRight = 1 And i <= shiftBy) Or _
           (shiftToRight = -1 And i + shiftBy >= Len(x)) Then
            resultStr = resultStr & "0"
        Else
            resultStr = resultStr & Mid(x, i - shiftToRight * shiftBy)
        End If
    Next i
    End Function
    These are the function prototypes for the intToBinStr and binStrToInt functions I used:
    Code:
    Public Function intToBinStr(intVal As Integer) As String
    Public Function binStrToInt(binStr As String) As Integer
    Sorry I couldn't finish that off but I have to go now. I can't remember if VB has a Bin() function to convert to binary, and I don't have the documentation here to check it. I'll see what I can do when I get home to my computer.

    Anyway hope that code is close to working, good luck. If you need any help just say
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

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



Click Here to Expand Forum to Full Width