Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Detect set bits

Posted on 2004-04-14
Medium Priority
295 Views
I need to determine if bits 0, 1, 10 and 11 are set
in unsigned short that is passed to me.

I wrote the code, but there's no way for me to test!
Need to make sure I'm getting it right.
Value passed to me will something like 3515, or so I was told.

Plz help, urgent.
0
Question by:jd9288
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Author Comment

ID: 10827022
BTW, the code I have is this:

{

int dattest;

//
// shift desired bit into rightmost position
//
dattest = (dat >> bitno);

return (dattest & 1);

}
0

LVL 6

Assisted Solution

joghurt earned 600 total points
ID: 10827067
unsigned short value = ...;

if ((value & (1 << 0)) != 0) {
// bit 0 is set
}
if ((value & (1 << 1)) != 0) {
// bit 1 is set
}
etc.

For bits 0, 1, 10 AND 11 (at the same time) you'd try (1 << 0) | (1 << 1) | (1 << 10) | (1 << 11).
You may be surprised but the above expression gives me 3075 (or 0x0C03) instead of 3515.
So for checking what you want you should write

if ((value & 0x0C03) == 0x0C03) {
// All the required bits are set.
}

OR

if ((value & 0x0C03) != 0) {
// At least one of the required bits are set.
}
0

LVL 46

Expert Comment

ID: 10827072
Hi jd9288,

#define BITMASK ((1 << 0) | (1 << 1) | (1 << 10) | 1 << 11))

if (Value == BITMASK)  /*  All bits in bitmask are set, no others are set  */

if (Value & BITMASK)   /*  All bits in bitmask are set, others may be set  */

if (Value & BITMASK)   /*  Any bit in bitmask is set  */

Good Luck,
Kent
0

LVL 46

Expert Comment

ID: 10827078
Whoops...

#define BITMASK ((1 << 0) | (1 << 1) | (1 << 10) | (1 << 11))

:-)

0

LVL 46

Expert Comment

ID: 10827098
Man, what a day...

#define BITMASK ((1 << 0) | (1 << 1) | (1 << 10) | (1 << 11))

if (Value == BITMASK)  /*  All bits in bitmask are set, no others are set  */

if ((Value & BITMASK) == BITMASK)  /*  All bits in bitmask are set, others may be set  */

if (Value & BITMASK)   /*  Any bit in bitmask is set  */

0

LVL 4

Assisted Solution

bkfirebird earned 600 total points
ID: 10827105
>> I need to determine if bits 0, 1, 10 and 11 are set
does this mean you want  to check whether the four least significant bits of the number are set?
your ReadBit function looks fine ... you can check if a particular bit is set by calling .....

if (ReadBit(num, 1)) // bit is set
// do something

0

LVL 46

Accepted Solution

Kent Olsen earned 800 total points
ID: 10827164
You can test this easy enough.

#define BITMASK ((1 << 0) | (1 << 1) | (1 << 10) | (1 << 11))

unsigned int TestBit (unsigned integer Value)
{
unsigned int Result;

}

If the displayed value for Result is the same as for BITMASK, then all bits in the bitmask were set in the original word.

Kent
0

Author Comment

ID: 10827187
Thank you all.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn hoâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
###### Suggested Courses
Course of the Month4 days, 14 hours left to enroll