#Clearing Bits using BITWISE OPERATORS

29 messages · Page 1 of 1 (latest)

digital iron
#

can only use bitwise, no ifs, no mod

marble mauveBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

digital iron
#

ping!

polar granite
#

so would 101011 become 101000 and would 1111 become 0000 and would 10101 become 10100?

dire jolt
#

I don’t think this is possible, bit wise operations operate on individual bits so they have no way of differentiating if a bit is at the end or beginning

#

However if you allow yourself to use ctz (count tailing zeros), you should be able to do something like this

int n; //input
int rot_num = ctz(~n); // count trailing 1s
n >>= rot_num; // shift right
n <<= rot_num; // shift back left, replacing last rot_num bits with 0
#

In computer software and hardware, find first set (ffs) or find first one is a bit operation that, given an unsigned machine word, designates the index or position of the least significant bit set to one in the word counting from the least significant bit position. A nearly equivalent operation is count trailing zeros (ctz) or number of trailing...

#

There’s an implementation here maybe you could do something

simple pilot
#

while the last bit is set, shift one to the right

done

polar granite
#

@digital iron can you use loops at all? Because internally those use if

stable briarBOT
#
Program Output
32
#
Program Output
32
#
Program Output
32
acoustic storm
#

Interesting assignment. Feels like code golf.
I cannot do it without a loop.

dire jolt
#

Wow that’s very clever

acoustic storm
#

It is. I tip my hat to thee @simple pilot
Annoyingly, I had a voice in my head telling me there is a clever solution.
I am just not clever enough to come up with it myself.
I can only marvel at its simplicity when confronted with it.

dire jolt
#

Why did it get deleted

dire jolt
#

btw the original response from 5921 was x&(x+1)

polar granite
#

Dang that's genius

marble mauveBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

warped prawn
dire jolt
#

Ah, I didn’t know c++ added that

#

Cool 👍

marble mauveBOT
#

@digital iron

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

digital iron
#

yep done

#

thanks

marble mauveBOT
#

@digital iron Has your question been resolved? If so, run !solved :)

digital iron
#

!solved