You are not logged in. Please login at www.codechef.com to post your questions!

×

lowest set bit

why does n&!(n-1) for n=6 gives 0 in C++ and 2 in python . it should give 2 though .

asked 12 Sep '17, 22:42

junior_g's gravatar image

3★junior_g
444
accept rate: 0%


! is an invalid operator in python but in c++ it coverts a true statement to false and vice versa . The real compliment operator is ~ .

By convention in c++ every positive number is true in c++ and 0 is false .

So ! operator converts the positive number which is true into false which translates to 0 . And the AND operation with 0 yields 0.

link

answered 12 Sep '17, 23:07

trashmaster's gravatar image

2★trashmaster
98619
accept rate: 12%

Extract lowest set bit (BLSI) is -n & n. Wikipedia (Bit Manipulation) has a decent section on this and other bitwise operations.

link

answered 16 Sep '17, 02:20

kungfuboner's gravatar image

1★kungfuboner
254
accept rate: 0%

Answer is hidden as author is suspended. Click here to view.

answered 16 Sep '17, 08:31

ashikajain's gravatar image

0★ashikajain
(suspended)
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,917
×280
×77

question asked: 12 Sep '17, 22:42

question was seen: 505 times

last updated: 16 Sep '17, 08:31