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

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 7th, 2004, 10:44 AM
VKovac VKovac is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 4 VKovac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy 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
.........

Reply With Quote
  #2  
Old May 7th, 2004, 11:01 AM
Stack Overflow Stack Overflow is offline
C Programmer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 15 Stack Overflow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 15 m
Reputation Power: 0
Send a message via ICQ to Stack Overflow Send a message via AIM to Stack Overflow Send a message via MSN to Stack Overflow Send a message via Yahoo to Stack Overflow
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

Reply With Quote
  #3  
Old May 7th, 2004, 01:24 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 43 m 11 sec
Reputation Power: 1800
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 getCharBitchar byteint bitnum )
{
    return( (
byte & (0x01 << bitnum) ) != ) ;



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 03:30 PM. Reason: Added code comments

Reply With Quote
  #4  
Old May 7th, 2004, 02:52 PM
VKovac VKovac is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 4 VKovac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.
Vlado

Reply With Quote
  #5  
Old May 7th, 2004, 03:58 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 43 m 11 sec
Reputation Power: 1800
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 getCharBitbyte) ;
if( 
odd_byte )
{
    
cout << "Byte value is odd" << endl ;
}
else
{
    
cout << "Byte value is even" << endl ;



Clifford

Reply With Quote
  #6  
Old May 7th, 2004, 05:19 PM
VKovac VKovac is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 4 VKovac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Clifford,
Thanks a lot
It works fine....
Best Regards
Vlado

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC 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 
Rate This Thread:


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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap