DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2003
    Posts
    4

    read bit from byte

    I have this function which gets a byte as an integer and i want it to return the bit from the requested place .
    I got this code from someone but i don't understand what "input & (1 << bit) " does.




    code :

    /**
    * Check if selected bit is set in <code>input</code>.
    * @param input Value to check
    * @param bit Bit number to check (0..7 with 7 MSB)
    * @return 1 if bit is set, 0 otherwise
    */

    private int getBit(int input, int bit)
    {
    if ((input & (1 << bit)) > 0)
    {
    return 1;
    }
    else
    {
    return 0;
    }
    }

    }


    thanx in advance
    Narf

  2. #2
    Join Date
    Jan 2003
    Posts
    4
    it's ok, i have found it

    it wasn't so difficult after all

    (input & (1 << bit)) > 0


    the bit shifts the "1" in the byte(1) to the wanted location(given by bit)
    and then just compares it with that same position in the byte(input)
    and if it's greater then zero we know that the byte(input) has a "1" at that location


    greets
    Narf

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links