## Interview Question

• 1
of 1 vote

Country: United States

Comment hidden because of low score. Click to expand.
0
of 0 vote

(n & (1 << k)) ? (n ^ (1 << k)) : n

Comment hidden because of low score. Click to expand.
0
of 0 vote

(n & (1 << k)) ? (n ^ (1 << k)): n

Comment hidden because of low score. Click to expand.
0
of 0 vote

(~(1 << k)) & n

Comment hidden because of low score. Click to expand.
0
of 0 vote

``n & (!(1 << k))``

Comment hidden because of low score. Click to expand.
0
of 0 vote

Let's keep this example at 8 bits for simplicity.

Create another number i which will start at 1: i = 00000001
Shift it k times. (Let's say k = 3): i = 00001000
Negate it with XOR by performing i ^ Int.max: i = i ^ 11111111 = 11110111
Return i & number: 10100010

Swift 2.2

``````func flip(k: Int, number:Int) -> String {
var operand = 1
operand = operand << k
operand = operand ^ Int.max
return String(operand & number, radix: 2)

}

flip(0, number: 0b101) // "100"
flip(1, number: 0b101) // "101"
flip(2, number: 0b101) // "1"``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

(n & (1<<k)) ? (n - (1 << k)) : n

Comment hidden because of low score. Click to expand.
0
of 0 vote

(n & (1 << k)) ? (n - (1 << k)): n

Comment hidden because of low score. Click to expand.
0
of 0 vote

isnt it just:

\$n ^ (1 << \$k) ?

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.