Solved

Detect set bits

Posted on 2004-04-14
8
264 Views
Last Modified: 2010-04-15
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
Comment
Question by:jd9288
8 Comments
 

Author Comment

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

ReadBit(int dat, int bitno)
{

     int dattest;

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

     return (dattest & 1);

}
0
 
LVL 6

Assisted Solution

by:joghurt
joghurt earned 150 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 45

Expert Comment

by:Kdo
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 45

Expert Comment

by:Kdo
ID: 10827078
Whoops...

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


:-)

0
 
LVL 45

Expert Comment

by:Kdo
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

by:bkfirebird
bkfirebird earned 150 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 45

Accepted Solution

by:
Kdo earned 200 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;

  Result = Value & BITMASK;
  printf ("Test Value = %8.8x, Mask=%8.8x, Result=%8.8x\n", Value, BITMASK, 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

by:jd9288
ID: 10827187
Thank you all.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 177
Passing a array as parameter - C 2 95
Super Scope, DHCP 5 75
Autosar OS Multicore Share Resources confusion ? 2 34
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now