The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Reading bits of the binary number...
Discuss Reading bits of the binary number... in the C Programming forum on Dev Shed. Reading bits of the binary number... C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 7th, 2004, 10:44 AM
|
|
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
......... 
|

May 7th, 2004, 11:01 AM
|
|
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
|

May 7th, 2004, 01:24 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
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 03:30 PM.
Reason: Added code comments
|

May 7th, 2004, 02:52 PM
|
|
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.
Vlado
|

May 7th, 2004, 03:58 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
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
|

May 7th, 2004, 05:19 PM
|
|
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
Vlado
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|