Question

set 0x8675309F, %o1
sra %o1, 16, %o1

can someone please explain what sra is supposed to do here, i know it is the right shift arithmetic but what does it do in this instruction exactly. also i know how to convert hex numbers to decimal and from decimal to binary but is there a simpler way to do logic gate operations on hex numbers? like for example if i want to AND or OR or XOR two hex numbers, how would i go about doing that? i know how its done with binary, but is there a way to do that with hex or do you need to convert to binary and then do the logic gate operations on the said binary number?

here is my attempt: the answer will be 0x8675, so this is what i did (not sure if this is right) i converted the hex number to a decimal then from the decimal to binary, this is where i did the right shift operation shifting right by 16 places (or adding 16 zeros to the front of the binary number), then i converted to hex again. the problem is that to convert back to hex i had to use an online converter and to convert from decimal to binary i also used a converter (since the number is very large), the problem is that on an examination of any type i will not be able to use online converters, so what is the technique for the fastest way to solve such a problem?

what about doing an logical gate AND or XOR operation on hex numbers, would i have to convert to binary or can i preform the logic gate operation on the hex numbers in anyway?

Was it helpful?

Solution 2

Wether you performa logical operation with a binary or a hex or a decimal number format doesn't matter because the output will be the same. However, if you use hex represetation it has the advantage that you can more easily read the binary value in the source code then with dec. Reason is, because hexadimzmal has a a bas which is a multiple of base 2, thus you can easily visually identify a four digit binrary ina hex number, while this is not so easy in decimal format.

As an example:

 3ab9h   ->   3      A     B     9
             0011  1010  1011  0101

So each digit in Hex is a four digit binary, which is easy enough to handle.

OTHER TIPS

Hexadecimal is a textual format for a number, not a property of the number itself. As such hex has no bearing on the meaning of any machine operation you might want to perform on a number.

Arithmetic right shift is a right shift that shifts ones into the vacated high bits if the most significant bit is set (otherwise, zeros). This has the effect of preserving the sign of a quantity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top