Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
May 7th, 2004, 11:44 AM
 VKovac
Registered User

Join Date: May 2004
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
Reading bits of the binary number...

Hi to everybody...

I need to read independently each bit of a binary number (1 byte). Can anybody help me to solve this problem?
I will appreciate very much your help
.........

#2
May 7th, 2004, 12:01 PM
 Stack Overflow
C Programmer

Join Date: May 2004
Posts: 15
Time spent in forums: 5 h 15 m
Reputation Power: 0
Hi,

Do you mean like binary of the letter 'Z':

01011010

For reading each of the binary bytes like '0' '1' '0' '1' '1', etc...?

- Stack Overflow

#3
May 7th, 2004, 02:24 PM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,975
Time spent in forums: 1 Month 4 Days 5 h 13 m 15 sec
Reputation Power: 1801
To detemine the state of an individual bit within an integer (of any type), you need to create a mask, with the required bit set and all other bts set to zero. This is most easily generated by shifting the value 0x01 by the bit number where 0 represents the least significant bit. The mash is then bitwise AND'ed with the integer in question, the result is non-zero (specifically equal to the mask) if the bit is set, otherwise the result is zero.

It is common to implement such a test in a macro, this has the advantage of allowing any type not just char to be used, but in general macros are best avoided, so a function is better. If you want to make a generic function to deal with any integer type, then use a C++ template function, or build a function for the largest integer type requiredand use implicit or explicit type casts for smaller types. If you make the function inline in C++, there is no overhead compared with a macro, but the overhead is insignificant in most applications.

A simple C++ function, specifically for char type:

PHP Code:
``` // byte is value to be bit tested, bitnum is the bit to  // be tested, bitnum values 0...7 are valid. inline bool getCharBit( char byte, int bitnum ) {     return( (byte & (0x01 << bitnum) ) != 0 ) ; }  ```

You can do this in C, but bool is not a built in type, and inlining is not standardised or portable.

Clifford

Last edited by clifford : May 7th, 2004 at 04:30 PM. Reason: Added code comments

#4
May 7th, 2004, 03:52 PM
 VKovac
Registered User

Join Date: May 2004
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
Clifford, thanks a lot for your answer.....!!!!!!!
In theory, i understand your point, but I am new with c++ and I am bothering you with a little bit more complete example of your code.. ( c++ function)
I will appreciate that.

#5
May 7th, 2004, 04:58 PM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,975
Time spent in forums: 1 Month 4 Days 5 h 13 m 15 sec
Reputation Power: 1801
Say the bit you were interested in was bit 4. The mask required would be 00010000B (conventionally the bits are numbered from 0, starting from the right - the least significant bit).

To generate this mask from a variable so that you can select any bit, you use the bit pattern 00000001N and shift it left by the bitnum where bitnum is the variable that specifies the required bit.

So if bitnum is 4, 00000001B shifted left bt 4 is 00010000B. In C/C++ the expression is 0x01 << 4. You need not use hex (0x) notation, but this is convenient because a single hexadecimal digit corresponds to exactly four binary bits, so it is easy to convert in your head (with practice and experience), so 0x01 << 4 is equal to 0x10 which equals 00010000B. (Note there is no binary notation in C, only hexadecimal, octal, and decimal, and you can largely forget octal).

Having created a mask, 0x10 using bitnum, we bitwise AND it to the byte we are bit testing. The AND operation produces a result of 1 is both bits are 1, and zero otherwise. So for example if the byte value was 0x5a (01011010B):
Code:
```      0x5a    01011010
AND   0x10    00010000
----------------------
=     0x10    00010000
----------------------```

Now if the byte to be tested was 0xa5 (10100101B):
Code:
```      0xa5    10100101
AND   0x10    00010000
----------------------
=     0x00    00000000
----------------------```
So simply testing for zero or non zero returns a boolean that is true if the bit in question is set or zero otherwise. The function I posted earlier may be used as follows:
PHP Code:
``` // test least significant bit to determine if odd or even byte value bool odd_byte = getCharBit( byte, 0 ) ; if( odd_byte ) {     cout << "Byte value is odd" << endl ; } else {     cout << "Byte value is even" << endl ; }  ```

Clifford

#6
May 7th, 2004, 06:19 PM
 VKovac
Registered User

Join Date: May 2004
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0
Clifford,
Thanks a lot
It works fine....
Best Regards

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Reading bits of the binary number...

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |