##### How to get a bit from a number, and set it into another number

I'm trying to make a function that accepts two unsigned integers by values (a, b), then sets the (N)th bit in (a) to the value of the (N)th bit in (b).

I ended up with this pseudo-code:

```
N = 1
f(a, b) {
sign = (b >> N) & 1
return a | sign
}
```

My attempt is wrong. I've been looking around at bit-twiddling articles, blog posts, and SO posts, but still don't see why it's wrong.

Does anyone know how to fix it?

I'm thinking that it should be `a | sign << N`

to get the bit into position, but that didn't work.

**Voters**

well, apparently rust doesn't like constant ternaries???

maybe make bit position a function instead

@Coder100 No, the code works fine on an up-to-date Rustc, but the pseudo-code is wrong, so it fails at runtime.

Try https://play.rust-lang.org, to see it yourself.

But the Rust is just an implementation detail and use, the pseudo-code example is all of the bitwise operations that matter.

It just looks like the `if`

statements are throwing syntax errors so I can't really see the problem...

Can you convert the number to a string of bits and perform math that way (AKA: `5`

to `101`

and replace the last `1`

with a bit from another number)?

Can you convert the number to a string of bits and perform math that way (AKA: 5 to 101 and replace the last 1 with a bit from another number)?

No strings, that is not math.

@xxpertHacker You never said in your post that it had to be with only math :/

@Baconman321 You cannot perform "math" on strings, I don't know what programmer or mathematician would say that you can.

I'm writing branchless code that operates on bits to emulate missing hardware instructions, but I'm told that I should go heap allocate two strings, change a character, parse the string into a number, free the strings, etc.

Like, wtf? That is how poorly performing garbage software is made. I don't need to be micro-optimizing in order to see that.

@xxpertHacker ok, well with the little details that I had from your post it was hard to see...

Oh, I see... my term "math" confused you (or not IDK).

Sorry... I meant to perform shifting and such using strings. Yes, I can see how that is slow.

It was merely a suggestion. Sometimes we are so focused on one problem that we don't see the solution because it is from another angle. And sometimes, looking at it from a different angle won't work as well...

If it doesn't work then it doesn't work...

@Baconman321 It would work, as it, technically the result would be accurate, but I usually have a performance baseline goal, and in this case, equivalent code could look like the following:

```
f(a, b) {
if b > 0 {
return a
} else {
return -a
}
}
```

@xxpertHacker Ok, I'll try to find out how to use bitwise manipulation in this case.

@19wintersp Do you know a think or two about bit mutation? Could you help me out?

@xxpertHacker I don't know how to do this efficiently, but I know you could do it if you know the size of the value. For example, here it is with two

`u8`

's, setting the last bit:That for the

nth bit is:And it should work like this:

This code isn't tested, and probably doesn't workI've just realised this is effectively the same as

@19wintersp Oh,

`mask & sign`

vs my current`sign << n`

, I'll try it later.@19wintersp Wait no, that makes no sense,

`v & 1`

can only be`0`

or`1`

, so`0b000100 & 1`

will just be`0`

, while`0b0001 & 1`

will be`1`

, thus the mask is wrong.@xxpertHacker That's what should happen, isn't it?

As an example, for getting the third bit:

`mask`

becomes`0b00000100`

`mask`

ANDed with something will extract that bit only`1`

if it's not already@xxpertHacker I wrote a quick test, and it seems to work properly:

@19wintersp This evaluates to

`a | mask`

, where mask is`0`

or`1`

, I don't see how that makes sense?`a | 0`

is`a`

,`a | 1`

sets the first bit, right? I want to set the Nth bit though?@19wintersp Hmm... odd, can you link your test?

@xxpertHacker

@19wintersp Okay, seems like it does work.

If you can try the following:

On an LE machine, you should get the following:

Indicating that the left-most bit is the signbit that changed.

I'm trying to target it with the following:

any guess as to why this isn't working?

@xxpertHacker Literally no clue, since it works for me:

Output:

@19wintersp Oh, I see the problem, if you have a negative and then a positive, it breaks: (-4.0, 4.0) = -4.0.

This is because 1 | 0 = 1, it doesn't set it if the second bit is 0. Maybe OR isn't the right operation?

@xxpertHacker No, it's because it's copying a bit from one thing to the other, it's directional, and so the argument order matters.

@19wintersp I'm not understanding how it's directional, can you elaborate on why the (-4.0, 4.0) results in (-4.0), if the bit was copied from the rhs' into the lhs?

@xxpertHacker Apologies, I misread the comment: you're right, it should be something else. Whilst this function

isdirectional, what I made is a broken implementation. Once again, I don't know how to do thiswell, but something like this might work (for a`u8`

):Edit: no, it doesn't, but this should:

@19wintersp Worked flawlessly. The pattern seems familiar and can probably be optimized but the mask will be constant, so it'll be good anyways. Thank you.