Determining the sign of an int using shifts? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here's the question
sign - return 1 if positive, 0 if zero, and -1 if negative
Legal ops: ! ~ & ^ | + << >>
Max ops: 10
Rating: 2
I thought of shifting to the right by 31 to grab the sign bit but can't 0 be positive AND negative technically?

You may try like this for 32 bit int:-
(x >> 31) | (((~x + 1) >> 31) & 1)

Related

C program: multiply two 32 bit value & return in 64-bit on a processor having a 16*16 multiplier [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In a recent interview, I was asked the following coding problem. Could someone please help me understand how to address this problem in terms of C coding?
Q.
A processor has a 16*16 multiplier to represent 32-bit value.
Take two 32 bit value, multiply them and return in 64 bit format.
Let's call the 32 bit numbers A and B. Now, let's split them into 16 bit numbers, a0/b0 and a1/b1 (a0 is the bigger part).
Now, A*B == (a0<<16+a1) * (b0<<16+b1) == (a0 * b0) << 32 + (a1 * b0 + a0 * b1) << 16 + a1 * b1.
note: All the multiplications here are for 16 bit numbers (a0,a1,b0,b1) with the result as 32 bit number casted into 64 bit (the bigger 32 bits of the number are 0).

How do you split a byte into sets of 2 and then rotate them using C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am currently learning bitwise operations in C.
Given a byte(spaced for readability): 11 01 11 10
Must turn it into: 10 11 01 11
How do I go about doing this?
Mask and shift:
int a = 0b11011110;
int b = ((a & 0b11000000) >> 6) |
((a & 0b00110000) >> 2) |
((a & 0b00001100) << 2) |
((a & 0b00000011) << 6);
I used binary constants here, but in "real life" you'll more often see hex - use whichever makes the most sense and reads best for your use case.
int b = ((a & 0xc0) >> 6) |
((a & 0x30) >> 2) |
((a & 0x0c) << 2) |
((a & 0x03) << 6);

Byte manipulation does not work with unsigned char [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I tried to experiment with bit manipulations on a byte.
At first I tried to say that I have 1111 1111 (256) and 1000 0000(128).
So I do this:
printf("%u\n", 256 & 128);
I expect to get 128 but I get 0.
So I tried:
printf("%u\n", ((unsigned char) 256) & ((unsigned char) 128));
But that gives me the same result.
What is wrong with that?
1111 1111 is 255
So try
printf("%u\n", 255 & 128);
^^^
Take into account that the type of the integer constants 255 and 128 is int.

Solving bitwise XOR and ROR equation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I have been told that x ^ ROR(x, 13) = 0x936f2a8247534566
^ is the XOR operator, like in C, and ROR() is a function that rotates-right the bits of the input by the specified number of positions, like the Intel processor instruction.
The question is how do I find x. It seems a lot of possibilities to try every 64-bit combination, maybe there is a better way?
This algorithm
unsigned long long res = 0;
int bit = 1;
for (int k = 0, shift = 0; k < 64; k++, shift = (shift + 13) % 64)
{
if (bit)
res |= 1ull << shift;
if (0x936f2a8247534566 & (1ull << shift))
bit = 1 - bit;
}
gives the answer
0x1337b33fdeadb00b
And if we start start with bit = 0, the answer is
0xecc84cc021524ff4
The idea is the following. If the last bit of 0x936f2a8247534566 is 0, it means that bit[13] ^ bit[0] == 0, hence bits are equal. Otherwise bit[0] and bit[13] are different.
The same logic applies to bit[13] and bit[26], etc. So basically the number 0x936f2a8247534566 tells us which bits of the original number are equal to each other and which are not.
Since with step 13 we get all possible positions between 0 and 63(inclusive), we need just one loop.

(*(pucBuf)++) = (unsigned char) (usValue >> 8); [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I got this C language code from the client,which is about message encapsulation,but I don't have a good understanding of this code.
Can someone please explain what it's doing?
(*(pucBuf)++) = (unsigned char) (usValue >> 8);
can be read as:
get the value of usValue and right-shift it 8 bits;
cast that to an unsigned char type;
store it into the memory location pointed to by the pubBuf pointer;
advance pucBuf to point to the next sequential item of its type.
Most likely it's taking the high-order eight bits of a sixteen(-or-more)-bit value and storing that into a memory buffer, within some sort of loop.

Resources