Doubts in quiz of bitmasking

I have mentioned the questions in which i have doubts . Kindly help me figure out the right answers with a bit of explanation
Q12. bitmask ~
int x = ~10;
What will be the value of x?

10

-10

-11

11

Q9. bitmask 1<<32 32 bit
long long x = (1 << 32) ;
What will be the value of x ?

2^32

Compilation Error

2^31

None of the above

Q7. bitmask power of 2

bool get(int x)
{

return (x && !(x & (x - 1)));

}
Above function will return :-

false if X is a power 2

true if X is a power 2

true if X is a perfect square of some integer

true if X is a not a perfect square of some integer

Q4. bitmask index of on lsb
Which one line function can be used to get index of the least significant bit that is on (first 1 from right to left in binary representation of an integer) ?

log2( N ^ -N ) + 1

log2( N | -N ) + 1

log2( N & -N ) + 1

log2( N ~ -N ) + 1

Question

bitmask index of on lsb

Which one line function can be used to get index of the least significant bit that is on (first 1 from right to left in binary representation of an integer) ?

Solution:
N&-N will return the place value of lsb
now if you take log and add +1 it will definetly give you index of lsb

Question

bitmask power of 2
bool get(int x)
{
     return (x && !(x & (x - 1)));
}

Above function will return :-

Choices
  • false if X is a power 2
  • true if X is a power 2
  • true if X is a perfect square of some integer
  • true if X is a not a perfect square of some integer

Solution

(x&(x-1)) this will give you the number after removing
last set bit
now if number is in power of 2 like 2,4,8,16 then it contains only 1 one set bit hence after removing last set bit these nos become 0
so

!(x & (x - 1))

will give true if number is in the power of 2 and false else not
but 0 will also give true
hence we also take and with number

question

long long x = (1<< 32) ;

Solution
this will give compilation error
as 1 is int so contains only 32 bits hence you can left shift till 31 only

if you want to left shift one by 32
first make 1 as long long
write like long long x=(1LL<<32);

if you want to ask something about this feel free to ask
i hope this helps
if yes show your response with :heart: and don’t forgot to mark doubt as resolved