Number the bytes in a 32-bit word from 0 (least significant) to 31 (most significant). Write C code that will return a bitmask containing 1s for the least significant n bits and 0s for the remaining most significant bits.
USING BITWISE OPS AND SUBTRACTION ONLY. Must handle input 0 - 32 inclusive. Can't shift by 32
Kevin Murphy
are you a nigger?
Blake Edwards
0xFFFFFFFF >> 32-n doesn't work if input is 0 because can't shift by 32.
0x2
Julian Bennett
Will shift operations be acceptable as bitwise operations?
If now you could use an ugly case statement with 32 entries.
James Young
>Number the bytes in a 32-bit word from 0 (least significant) to 31 (most significant). What? Bits or bytes? There are only 4 bytes in a 32 bit word.
Xavier Campbell
Lips are disgusting.
Jaxson James
Operations that are allowed are shifts, subtraction, and/or/not/xor. No other operations allowed, such as loops or if statements
Anthony Turner
Sorry typo number the bits in a 32 bit word
Adrian Hall
0xFFFFFFFF >> (31-n)
n=0: mask for bit 0 only which is LSB and mask = 1 n=31: mask for all bits, no shifts, and mask = 0xFFFFFFFF
Levi Ortiz
>Must handle input 0 - 32 inclusive. t. not op
Aiden Lopez
I'd call that a big in the specs.
t.Former QA auditor
Samuel Ramirez
Well, since bit 32 has no meaning what is the desired output for input 32?
Hunter Watson
Why? Clearly op wants that for input n = 0 the mask should be all zeros and for n = 32 it should be 0xFFFFFFFF
Evan Brown
see
Isaiah Powell
>Number the bytes in a 32-bit word from 0 (least significant) to 31 (most significant).
for(int n = 31; n >= 0; n--) { printf("%d ",n); }
1/2
Henry Peterson
Mask for the first n bits n=0 -> 0x0 n=2 -> 0x3 n=32 -> 0xFFFFFFFF
Jace Allen
bool[] word = {t,t,f,f,f,t,t,f,t,f,f,f,f,t,t,f,f,t,t,f,f,t,t,f,t,f,f,f,f,t,t,f}; int n;
n=1: mask for bit 0 only which is LSB and mask = 1 n=32: mask for all bits, no shifts, and mask = 0xFFFFFFFF
That leaves the problem of n=0 and not shifting by 32. That constraint can be enforced as
0xFFFFFFFF >> ((32-n) and 31) That is half way solved because we avoid excessive shifts but mask for n=0 is now same as for n=32. We can fix that by and x where x is constrained to 0 for n=0 (easy) and x=1 for n>0 (hard)
Thinking...
However n is even or odd. so x = (1 and n and (n or (n-1))
Chase Morris
There's a MMX x86 instruction that does this (supposedly). Are you an Intel engineer trying to fix the meltdown bug perchance?
Aaron White
A small bug, still 32 shifts for n=0. Gotta think more...
Jaxson Williams
#include
int add (int a, int b) { return 0 - (0 - a) - (0 - b); }
int twice (int a) { return add (a, a); }
int succ (int a) { return add (a, 1); }
int gimme (int n) { int i; int a; a = 0; for (i = n - 1; i >= 0; i--) { a = twice (a); a = succ (a); } return a; }
int main (void) { int n; for (n = 0; n
Carter Jackson
( 0xFFFFFFFF >> ((32-n) and 31) ) and (1 and n and (n or (n-1))
(this is getting hideous! Must be Intel!!)
No, n=0 becomes 32 and 31 which is zero. No shifts. Then and'ed with zero. The whole mess becomes zero.
MMX = Meltdown Mega eXtension
Jayden Perry
problem doesn't make sense, how can it be inclusive to n = 32 if the bits themselves are numbered 0...31?
Jordan Bailey
It is poorly worded. Here is the expected output: Mask for the first n bits n=0 -> 0x0 n=2 -> 0x3 n=32 -> 0xFFFFFFFF
Gavin Phillips
>Can't shift by 32 x >> 16 x
Jaxson Long
No just a brainlet cs undergrad
Joseph Young
Still wrong. The second term has to be either 0 or 31 instead of either 0 or 1.
Connor Ortiz
x = (1 and n and (n or (n-1)) That is 0 or 1
y = (32 - x) and 31 x=0 y = (32 and 31) = 0 x=1 y = (31 and 31) = 31
OK, that is the ticket. Now put it all into place.
( 0xFFFFFFFF >> ((32-n) and 31) ) and (( 32 - (1 and n and (n or (n-1))) ) and 31)
I think that should do the trick.
Hudson Long
What reading material is there out there for learning practical/real world C or C++? That is up to date.
Hudson King
I read KnR and then started programming. It worked as well then as it will today.
Joshua Flores
the brainlets from Sup Forums are leaking again...
Joseph Carter
What, do they make programs now?
Jackson Fisher
who is this spooge bouje?
Liam Lewis
I wish I knew, friendo.
Landon Morgan
Sarah McDaniel
Dominic Thompson
That's a pretty sexist picture, OP You need to get the fuck out until you grow the fuck up
Thomas Diaz
uint32_t function(uint8_t n) { if (n == 32) return ~0; //Just replace this with a fixed value else return (1