Tfw more than half of the undergrads in my section can't do this

>tfw more than half of the undergrads in my section can't do this

You have two numbers, X and Y.

Using bitwise operations and loops, write code to determine how many bits must be flipped in order to turn X into Y.

e.g.
X: 101001
Y: 100101
^ ^
answer: 2 bits

If you can't do it in C, don't bother.

Other urls found in this thread:

docs.python.org/3/library/stdtypes.html#str.isdigit
twitter.com/SFWRedditGifs

I haven't touched c since kindergarten.
int z = x ^ z;
int count = 0;
for (count = 0; z != 0; count++)
z &= z - 1;

Something like that?

i would bruteforce the shit out of it

this is bretty gud. how did you get code tags?

actually there's a mistake in that first line

Dumb fuck solution.
int res=0;
while(x+y!=0){
if(x&1!=y&1)
res++;
x>>=1;
y>>=1;
}

>res=0
>x&1!=y&1
>x>>=1
>y>>=1
why do people do this
what style guide encourages this

I only know this because I was reading about the POPCOUNT function recently for no particular reason.

>implying I'm following any style
I'm self-taught and it's not my job.

*POPCNT

#include

unsigned bitflips_needed(unsigned a, unsigned b) {
unsigned flips = 0;
unsigned xored = a^b;

unsigned mask = 1;

for (unsigned i = 0; i < sizeof(mask)*8; i++) {
unsigned masked = xored & mask;
if (masked > 0) {
flips++;
}
mask

int z, count = 0;
for (z = x^y; z != 0; z >>= 1)
count += z & 1;
return count;

Brian Kernighan’s Algorithm

People who spend too much time in shell scripts, vim config files, and makefiles

function flipsNeeded(a, b) {
if (a == null || b == null)
throw new Error("2 arguments needed");
a |= 0;
b |= 0;
let xor = a ^ b;
let result = 0;
if (xor !== 0)
while (xor) {
++result;
xor >>>= 1;
}
return result;
}

What the hell is wrong with everyone ITT.
That's the difference between Practical Entreprise and CS.
I just googles how to counts bits in integer and wrote basically exactly this
Everyone else is making some shitty self made version that runs like shit, and are missing the chance to learn Brian Kernighan’s Algorithm.
Sometimes you have to use the resources given to you and not reinvent the fucking wheel

Couldn't you just xor and count the 1s?

Xor them and then literally add up all the 1's in base 2 form?

Which is more profitable? Node or C#?

You wont have google on hand every time.
Like whiteboard excercize.

>not using __builtin_popcount to count bits
ISHYGDDT

>subjecting yourself to whiteboard torture
Why don't I just get a cuckshed while I'm at it?

node: hipster startups
c#: enterprise companies

not sure which is more profitable desu.

c# it is i guess

>Being so incompetent you can't write basic algorithms of code snippets without googling it 6 times

>then you end up using a high level language with a function called count_xor_bits

Why is Python so good, lads?
x = int('101001', 2)
y = int('100101', 2)

bits_flipped = sum(
1 for b1, b2 in zip(
format(x, 'b'),
format(y, 'b')
)
if b1 != b2
)
print("Bits flipped:", bits_flipped)

#include

int main() {

int x, y, counter = 0;

scanf("%d%d", &x, &y);

for(int i = 0; i < sizeof(int) * 8; i++) {

int bitX = (x & ( 1 > i;
int bitY = (y & ( 1 > i;

printf("%d\n", bitX);

if(bitX != bitY) {
counter++;
}
}

printf("%d\n", counter);
}

that's what I came up with

>hipster startups

The int z =z part?

Zsolti?

Hajnali 5 van bazdmeg, menj te is aludni

Nem lehet, megyek túrázni. Ami másnaposan szopás lesz, de ez van

>Using bitwise operations and loops, write code to determine how many bits must be flipped in order to turn X into Y.

Give me two fucking uses for this. Programming challenges have zero value.

How do you do the code in OP's pic though?
Someone can probably do it in 4 lines without using parse.

int strikes=0;
for(int i=0; i

> Hey Sup Forums I bet you cannot do this very specific thing
> If you can't do it in C, don't bother.

"Do my homework for me", the post.

I believe it's the int part. int declares z, then assigns to it the undefined value of z xored with x.

sorry you can't do it sweetie

Uh.... you never heard of regular expressions?

__builtin_popcount(x^y)

Declare variable z, assign it a value of 0. Assign the value of x into an array, do the same for y. Compare each array position to each other, if they aren't equivalent in magnitude, add +1 to Z. Display output of z concatenated with whatever message you want when ran.

I'm not a code monkey to do this shit.
I'd better pay pajeets some shit for doing it for me.
I'd better spend more time for engineering in MATLAB.

>do my homework Sup Forums
come on now OP, the problem isn't even remotely challenging or interesting

t. guy who didn't post his own solution

Nobody uses c# for enterprise.

xor then popcnt
wow

u8 x = 0b101001;
u8 y = 0b100101;
int bits = sizeof(x) * 8;
int bitsToFlip = 0;
for (int i = 1; i > (bits - i) & 1)
!= (y >> (bits - i) & 1))
bitsToFlip++;
}
printf("Bits to flip: %u\n", bitsToFlip);

>X: 101001
>Y: 100101
for n=1:length,
c+=abs(X(i)-Y(i))
end

What world do you live in?

coding binary representations of values as ASCII strings and then working with them as they are arrays composed of 8bit character codes is also good, but too much bloat for such simple task. '0' and '1' codes differ between each other by 1 in the ASCII table, so no extra division is required.

on the other hand, using C for tasks that require simple loops with shifts and checking of carry flag is unnecessary

Here is my undergrad-tier solution.

I've seen some better ones in the thread, but this is honestly what I would have submitted back in the day.

- optimize karnaugh map
- detect errors in hamming encoding
- break xor encoding

None of those things are required for regular ass dev positions that 98% of coders occupy. Try again.

>C
>loops
lmao

>Using bitwise operations and loops
Why?

stand back, plebs

int CountFlips(int ayy, int lmao) =>
Convert.ToString(ayy, 2).PadLeft(32, '0')
.Select((x, i) => (x, i))
.Where(x => x.x != Convert.ToString(lmao, 2).PadLeft(32, '0')[x.i])
.Count();

>int z = x ^ z;
lol

In C/C++? Pic related. Figure it out.

>everyone posting long as shit solutions
>this works just fine:
Simpler is better

int xnor = ~(x ^ y)
int result = __builtin_popcount(xnor);


It's like 3 lines of assembly, popcount is usually done in hardware so there you go.

Scrubs
is_numeric($input);

that fucking image, my sides

suck my dongus brainlets
require("count-bits")(x^y)

Technically that's not part of any programming language. It's gcc specific.

Does x86 have a specific instruction to count bits?

Reminder that storing it in a lookup table is much more efficient at the cost of upfront processing time and memory. To build the lookup table and incidental use, use Brian Kernighan's algorithm.

bitcount[15] == 4
bitcount[16] == 1


For example.

Wait, what's the ~ for? Don't you want to popcount the bits not in common?

Technically this is right, but practically speaking, most relevant compilers support some sort of popcount. I think I saw some examples of it handled by a bunch of #ifdef COMPILER's

z does have a value retard. The residual value that may be read from the memory where that variable is created. The fact that you don't control that value just makes it deep.

>PENIS
"that's a number and a word!"

nice program, user

thatsthejoke.png

fuck logic
function fuck(x, y) {
if (x > y){
b = x
s = y
}
else{
b = y
s = x
}
b = [...b.toString(2)]
s = [...s.toString(2)]
while(b.length != s.length)
s.unshift('0')
c = 0
b.forEach((a, n) => {
if (a != s[n])
++c
})
return c
}

eh...

>OP daring Sup Forumstards into doing his homework

too lazy to write C but
z := X ^ Y
count := 0
for (mask := 0x1; mask != 0x0; mask

This works incorrectly if x or y is signed and your compiler sign extends on a right shift.

It's an exercise to determine whether you can figure out how to do it.

Hey uhm..I'm new to programming and I've been learning python. I'm trying to make the program in OP's post work but how do you get python to tell the user if what they're written is either a string or an integer? I'd tried a couple different things but I can't get it to work...

do I lose?
int distance(int x, int y) {
int diff = x ^ y;
int sum = 0;
int mask = 0x01010101;

for (int i = 0; i < 8; i++) {
sum += (diff & mask);
diff >>= 1;
}

return (sum + (sum >> 8) + (sum >> 16) + (sum >> 24)) & 0xFF;
}

I assume this is bait, but on the offchance it isnt:

docs.python.org/3/library/stdtypes.html#str.isdigit

so; if/(myString.isDigit()) etc etc etc

guy lives in memeworld. living in south eastern USA. at least 40% of job postings require C#.