Results 1 to 10 of 10

Thread: Testing for Bit Value of Binary Numbers

  1. #1
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default Testing for Bit Value of Binary Numbers

    Hi - I'm stuck again,

    How do I test the various bits [values (0 or 1)] of binary numbers? (I can't find the php method so far.....) For example, I need to know if bit n is true or false, set or not set. (I do know how to move bits and compare binaries, etc., but that does not help me with what I need to do.)

    I've decoded Hex into Binary with the function:
    Code:
    function hex2bin($source)
      {
      $strlen = strlen($source);
      for ($i=0;$i<strlen($source);$i=$i+2)
        {
        $bin .= chr(hexdec(substr ($source, $i,2)));
        }
      return $bin;
      }
    (Various logic operations will be based on a look-up and test of the retrieved & converted Hex strings. I've used Hex because I can read/edit them with a MySQL database editor)

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Since the returned binary value is a string, you can use PHP to access each bit as an array index.
    Code:
    function getBitValue($strBits, $flagNum) {
      return $flagNum < 0 ? ($strBits[(-1 * $flagNum) - 1] === '1') :
        ($strBits[strlen($strBits) - $flagNum - 1] === '1');
    }
    
    getBitValue('0010111100101', 0); // true
    getBitValue('0010111100101', 1); // false
    getBitValue('0010111100101', -1); // false
    getBitValue('0010111100101', -3); // true
    getBitValue('0010111100101', -201); // false
    Last edited by Twey; 08-28-2006 at 03:53 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Hmm... That's clever.
    - Mike

  4. #4
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    I like it, but I must have something wrong in the hex2bin function because 31 should return 110001 to your function, and getBitValue returns 'NaB'; indicating that the input string is not binary. uggh!

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I'm not sure why you're doing all that :-\ What's wrong with baseconvert($num, 16, 2); ?
    Last edited by Twey; 08-29-2006 at 02:38 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    The function is actually base_convert

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes. It is.

    Don't we love PHP and its naming inconsistencies.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Of course. lol

  9. #9
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    Twey, I had to laugh at myself when I read your message about base_convert(). What an easy solution! Thanks, obviously I need to spend more time reading the php man pages.

    BTW, you need to left-pad with 0's because the leading 0's are stripped from a binary output string - strange.....

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Which is why I altered my function to remove the "NaB" stuff and simply assume that everything before the beginning of the string is a 0
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •