sebi
sebi

Reputation: 837

In c how can I detect if any bit of a byte equal 0

I was able to check for 1's using (x && 0xf), but I have no idea how to detect 0's.

check_zeros(int y){


} //return 1 if 0 is found

Upvotes: 1

Views: 3239

Answers (3)

selbie
selbie

Reputation: 104549

As you have your function declared, y is not a byte, it's an integer. So I assume you want to find out if ANY bit in y is zero.

int check_zeros(int y)
{
    return ((~y) != 0);
}

Upvotes: 2

Alexey Frunze
Alexey Frunze

Reputation: 62068

Supposing it's an 8-bit value (from 0 to 0xFF):

int check_zeros(int y) {
    return y < 0xFF;
    // alternatively: return y != 0xFF;
}

Upvotes: 2

Greg Hewgill
Greg Hewgill

Reputation: 993243

You could do this:

int check_zeros(int y) {
    return (y & 0xff) != 0xff;
}

The 0xff represents a byte with all bits set to 1.

Upvotes: 6

Related Questions