>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.
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
>>62451574
this is bretty gud. how did you get code tags?
>>62451574
>>62451750
actually there's a mistake in that first line
>>62451108
Dumb fuck solution.int res=0;
while(x+y!=0){
if(x&1!=y&1)
res++;
x>>=1;
y>>=1;
}
>>62451812
>res=0
>x&1!=y&1
>x>>=1
>y>>=1
why do people do this
what style guide encourages this
>>62451108
I only know this because I was reading about the POPCOUNT function recently for no particular reason.
>>62451985
>implying I'm following any style
I'm self-taught and it's not my job.
>>62452000
*POPCNT
>>62451108#include <stdio.h>
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 <<= 1;
}
return flips;
}
int main() {
unsigned a = 0x29;
unsigned b = 0x25;
printf("%u\n", bitflips_needed(a, b));
return 0;
}
The only method that quickly popped to mind was "xor them, count the ones by anding with each possible single-one mask". I'm self-taught, so I'm sure there's a neater/more efficient way.
int z, count = 0;
for (z = x^y; z != 0; z >>= 1)
count += z & 1;
return count;
>>62451574
Brian Kernighan’s Algorithm
>>62451985
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 >>62451574
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
>>62451108
Couldn't you just xor and count the 1s?
>>62451108
Xor them and then literally add up all the 1's in base 2 form?
Which is more profitable? Node or C#?
>>62453545
You wont have google on hand every time.
Like whiteboard excercize.
>not using __builtin_popcount to count bits
ISHYGDDT
>>62453774
>subjecting yourself to whiteboard torture
Why don't I just get a cuckshed while I'm at it?
>>62453745
node: hipster startups
c#: enterprise companies
not sure which is more profitable desu.
>>62453818
c# it is i guess
>>62453807
>Being so incompetent you can't write basic algorithms of code snippets without googling it 6 times
>>62453828
>then you end up using a high level language with a function calledcount_xor_bits
>>62451108
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)
>>62451108#include <stdio.h>
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 )) >> i;
int bitY = (y & ( 1 << i )) >> i;
printf("%d\n", bitX);
if(bitX != bitY) {
counter++;
}
}
printf("%d\n", counter);
}
that's what I came up with
>>62453818
>hipster startups
>>62451796
The int z =z part?
>>62453545
Zsolti?
>>62454321
Hajnali 5 van bazdmeg, menj te is aludni
>>62454329
Nem lehet, megyek túrázni. Ami másnaposan szopás lesz, de ez van
>>62451108
>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.
>>62451108
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<input.length(); i++)
if((input.at(i)<48 || input.at(i)>57) && (input.at(i) != '.' || strikes++ == 1)){
strikes=2;
break;
}
if(strikes<2) cout << "number";
else cout << "not number";
>>62451108
> Hey /g/ 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.
>>62454304
I believe it's the int part. int declares z, then assigns to it the undefined value of z xored with x.
>>62455264
sorry you can't do it sweetie
>>62455344
>>62451108
Uh.... you never heard of regular expressions?
>>62451108__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.
>>62451108
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.
>>62451108
>do my homework /g/
come on now OP, the problem isn't even remotely challenging or interesting
>>62455654
t. guy who didn't post his own solution
>>62453818
Nobody uses c# for enterprise.
xor then popcnt
wow
>>62451108u8 x = 0b101001;
u8 y = 0b100101;
int bits = sizeof(x) * 8;
int bitsToFlip = 0;
for (int i = 1; i <= bits; i++) {
if ((x >> (bits - i) & 1)
!= (y >> (bits - i) & 1))
bitsToFlip++;
}
printf("Bits to flip: %u\n", bitsToFlip);
>>62451108
>X: 101001
>Y: 100101
for n=1:length,
c+=abs(X(i)-Y(i))
end
>>62455905
What world do you live in?
>>62456320
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.
>>62455264
- optimize karnaugh map
- detect errors in hamming encoding
- break xor encoding
>>62457930
None of those things are required for regular ass dev positions that 98% of coders occupy. Try again.
>C
>loops
lmao
>>62451108
>Using bitwise operations and loops
Why?
>>62451108
stand back, plebsint 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();
>>62451574
>int z = x ^ z;
lol
>>62455344
In C/C++? Pic related. Figure it out.
>everyone posting long as shit solutions
>this works just fine:
>>62451812
Simpler is better
>>62451108int 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.
Scrubsis_numeric($input);
>>62460210
that fucking image, my sides
suck my dongus brainletsrequire("count-bits")(x^y)
>>62460210
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.
>>62460210
Wait, what's the ~ for? Don't you want to popcount the bits not in common?
>>62460505
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
>>62455362
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.
>>62451108
>PENIS
"that's a number and a word!"
nice program, anon
>>62461856
thatsthejoke.png
fuck logicfunction 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
}
>>62462154
eh...
>OP daring /g/tards into doing his homework
too lazy to write C butz := X ^ Y
count := 0
for (mask := 0x1; mask != 0x0; mask <<:= 1) {
if mask & z != 0x0 {
count +:= 1
}
}
return count
>>62451812
This works incorrectly if x or y is signed and your compiler sign extends on a right shift.
>>62453545
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;
}
>>62467757
I assume this is bait, but on the offchance it isnt:
https://docs.python.org/3/library/stdtypes.html#str.isdigit
so; if/(myString.isDigit()) etc etc etc
>>62456348
guy lives in memeworld. living in south eastern USA. at least 40% of job postings require C#.