VB .NET Statement Help

Posted on 2011-05-05
Last Modified: 2012-05-11
Can someone please explain what this code is doing?  I've never seen the "<<" before and it looks like it's converting a true/false condition to an integer?
Dim seed As Integer = ((randomBytes(0) And &H7F) << 24 Or _
                                randomBytes(1) << 16 Or _
                                randomBytes(2) << 8 Or _

Open in new window

Question by:nightshadz

    Author Comment

    It's an extract from this code...
    LVL 33

    Expert Comment

    Yes.  They're taking four values (randomBytes) and bitshifting them left (by 24, 16, 8 and 0 bits, respectively), and ORing them together to come up with a single, 4-byte value.

    Author Comment

    Thanks.  The bitshifting makes sense (have to do some more research on this to fully understand it) but I'm still not getting the Or part and how it's putting those numbers together.  Can you explain this further?
    LVL 33

    Accepted Solution

    The OR operator sets a bit on if any of the ORed values is true.  So 0 OR 0 = 0, 0 OR 1 = 1, 1 OR 0 = 1, 1 OR 1 = 1.

    What they're doing in the sample code is something line this

    1010 0000 0000 0000
    0000 1100 0000 0000
    0000 0000 0010 0000
    0000 0000 0000 0101
    1010 1100 0010 0101  <- The value of the other numbers ORed together.


    Author Closing Comment

    Wow, Thanks! If I could give you another million points I would!

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now