hey guys!!!

iam just working on speed of the codes..
and i just want to extract the last bit of the given variable.could u help me???
note:dont extract the bits by dividing it by 2:):)

Recommended Answers

All 7 Replies

i just want to extract the last bit of the given variable.

Which bit do you think is the "last bit"? The term is ambiguous as it could be interpreted as either the most significant bit or the least significant bit.

note:dont extract the bits by dividing it by 2

Let me guess: this is a restriction on the homework you so carefully didn't say this question is based upon? :icon_rolleyes:

lets say its the most significant bit:D

k i just want to check whether the no s even or odd using msb:)

you check the least significant bit to determine if it's even or odd, because it's 2^0 = 1.

thus you can shift the bits the proper direction and determine if the bit is set or not.

in case bits were never described this way to you:
[ ] [ ] [ ] [ ]
we have four bits here, each with a value of:
[2^3] [2^2] [2^1] [2^0]

least sig. bit is set? value = 1.
lest sig. and 2^1 = 3.

for a byte this continues to 2^7.

is this shifting faster than dividing it by 2 and checking

lets say its the most significant bit

k i just want to check whether the no s even or odd using msb

Apparently you don't understand the difference between most significant and least significant. :icon_rolleyes: If you're checking for even/odd, you test the least significant bit:

if ((value & 1) == 0) {
    // Even
}
else {
    // Odd
}

sorry that was lsb...k thanks:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.