>res=0 >x&1!=y&1 >x>>=1 >y>>=1 why do people do this what style guide encourages this
Noah Rogers
I only know this because I was reading about the POPCOUNT function recently for no particular reason.
Isaac Gonzalez
>implying I'm following any style I'm self-taught and it's not my job.
Jason Stewart
*POPCNT
Christopher Perry
#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
Joseph Perez
int z, count = 0; for (z = x^y; z != 0; z >>= 1) count += z & 1; return count;
Grayson Morgan
Brian Kernighan’s Algorithm
Benjamin Kelly
People who spend too much time in shell scripts, vim config files, and makefiles
Blake Edwards
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; }
Ayden Collins
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
Jackson Murphy
Couldn't you just xor and count the 1s?
Cameron Davis
Xor them and then literally add up all the 1's in base 2 form?
Brody Green
Which is more profitable? Node or C#?
Easton Evans
You wont have google on hand every time. Like whiteboard excercize.
Luis King
>not using __builtin_popcount to count bits ISHYGDDT
Landon Johnson
>subjecting yourself to whiteboard torture Why don't I just get a cuckshed while I'm at it?
Ryder Martin
node: hipster startups c#: enterprise companies
not sure which is more profitable desu.
Julian Cooper
c# it is i guess
Liam Mitchell
>Being so incompetent you can't write basic algorithms of code snippets without googling it 6 times
Colton Anderson
>then you end up using a high level language with a function called count_xor_bits
Dominic Fisher
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)
John Robinson
#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
Jaxon Ortiz
>hipster startups
Anthony Powell
The int z =z part?
Jaxon Torres
Zsolti?
Gabriel Cook
Hajnali 5 van bazdmeg, menj te is aludni
Jonathan Robinson
Nem lehet, megyek túrázni. Ami másnaposan szopás lesz, de ez van
Joshua Edwards
>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.
Andrew Robinson
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
Eli Jones
> 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.
Michael Stewart
I believe it's the int part. int declares z, then assigns to it the undefined value of z xored with x.
Austin Foster
sorry you can't do it sweetie
Christian Davis
Uh.... you never heard of regular expressions?
Cooper Johnson
__builtin_popcount(x^y)
Daniel Collins
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.
Dominic Russell
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.
Luke Mitchell
>do my homework Sup Forums come on now OP, the problem isn't even remotely challenging or interesting
Julian Harris
t. guy who didn't post his own solution
Andrew Morales
Nobody uses c# for enterprise.
James Thompson
xor then popcnt wow
Ryder King
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);
Colton Cook
>X: 101001 >Y: 100101 for n=1:length, c+=abs(X(i)-Y(i)) end
Nicholas Perry
What world do you live in?
Jaxson Long
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
Benjamin Anderson
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.
>everyone posting long as shit solutions >this works just fine: Simpler is better
Asher Rivera
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.
Jonathan Hughes
Scrubs is_numeric($input);
Camden Hughes
that fucking image, my sides
Grayson Hughes
suck my dongus brainlets require("count-bits")(x^y)
Oliver Martin
Technically that's not part of any programming language. It's gcc specific.
Does x86 have a specific instruction to count bits?
Anthony Nelson
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.
Hudson Cox
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
Noah Rodriguez
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.
Jason Anderson
>PENIS "that's a number and a word!"
nice program, user
Dylan Kelly
thatsthejoke.png
Hudson Mitchell
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 }
Jaxon Howard
eh...
Leo Jenkins
>OP daring Sup Forumstards into doing his homework
Christopher Wright
too lazy to write C but z := X ^ Y count := 0 for (mask := 0x1; mask != 0x0; mask
Andrew Walker
This works incorrectly if x or y is signed and your compiler sign extends on a right shift.
Jackson White
It's an exercise to determine whether you can figure out how to do it.
Nathaniel Bennett
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...
Jaxon Moore
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; }