Number the bytes in a 32-bit word from 0 (least significant) to 31 (most significant)...

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

are you a nigger?

0xFFFFFFFF >> 32-n doesn't work if input is 0 because can't shift by 32.

0x2

Will shift operations be acceptable as bitwise operations?

If now you could use an ugly case statement with 32 entries.

>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.

Lips are disgusting.

Operations that are allowed are shifts, subtraction, and/or/not/xor. No other operations allowed, such as loops or if statements

Sorry typo
number the bits in a 32 bit word

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

>Must handle input 0 - 32 inclusive.
t. not op

I'd call that a big in the specs.

t.Former QA auditor

Well, since bit 32 has no meaning what is the desired output for input 32?

Why? Clearly op wants that for input n = 0 the mask should be all zeros and for n = 32 it should be 0xFFFFFFFF

see

>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

Mask for the first n bits
n=0 -> 0x0
n=2 -> 0x3
n=32 -> 0xFFFFFFFF

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;

for(n = 0; n < 32; n++) {
if(word[n]) {
printf("1");
}
}
printf("\n");

for(n = 0; n < 32; n++) {
if(!word[n]) {
printf("0");
}
}

2/2

Do your own homework.

0xFFFFFFFF >> (32-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))

There's a MMX x86 instruction that does this (supposedly). Are you an Intel engineer trying to fix the meltdown bug perchance?

A small bug, still 32 shifts for n=0. Gotta think more...

#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

( 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

problem doesn't make sense, how can it be inclusive to n = 32 if the bits themselves are numbered 0...31?

It is poorly worded. Here is the expected output:
Mask for the first n bits
n=0 -> 0x0
n=2 -> 0x3
n=32 -> 0xFFFFFFFF

>Can't shift by 32
x >> 16
x

No just a brainlet cs undergrad

Still wrong. The second term has to be either 0 or 31 instead of either 0 or 1.

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.

What reading material is there out there for learning practical/real world C or C++? That is up to date.

I read KnR and then started programming. It worked as well then as it will today.

the brainlets from Sup Forums are leaking again...

What, do they make programs now?

who is this spooge bouje?

I wish I knew, friendo.

Sarah McDaniel

That's a pretty sexist picture, OP
You need to get the fuck out until you grow the fuck up

uint32_t function(uint8_t n) {
if (n == 32)
return ~0; //Just replace this with a fixed value
else
return (1