Unsitgned integers

Hello,

here is just a little program I wrote:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <stdio.h>
#include <limits.h>


int main()
 {

  printf( " The minimum of signed char is %d\n ", SCHAR_MIN );          */ -128 */
  printf( " The maximum of signed char is %d\n ", SCHAR_MAX );          */ 127 */
  printf( " The maximum of unsigned char is %d\n ", UCHAR_MAX);         */ 255 */
  printf( " The minimum of int is %d\n ", INT_MIN);                     */ -2147483648 */
  printf( " The maximum of int is %d\n ", INT_MAX);                     */ 2147483647 */
  printf( " The maximum of unsigned int is %d\n ", UINT_MAX);           */ -1 */
  printf( " The minimum of long is %ld\n ", LONG_MIN);                  */ -9223372036854775808 */
  printf( " The maximum of long is %ld\n ", LONG_MAX);                  */  9223372036854775807 */
  printf( " The maximum of unsigned long is %ld\n ", ULONG_MAX);        */ -1 */
  printf( " The minimum of short  is %d\n ", SHRT_MIN);                 */ -32768 */
  printf( " The maximum of short  is %d\n ", SHRT_MAX);                 */  32767 */
  printf( " The maximum of unsigned short  is %d\n ", USHRT_MAX);       */ 65535 */
   
  return 0;
  
 }
  
 



What was interesting to me is that shifting right by n bits on an unsigned integer has the effect of dividing it by 2^n (rounding towards 0).

So shifting left or right by unsigned binary number is more efficient than with signed, but could you give me an example how it would be used if at all?
So shifting left or right by unsigned binary number is more efficient than with signed,


Waaaat? What makes you say/think that?

but could you give me an example how it would be used if at all?


Use bitshift if you need to bitshift.
Use multiply/divide if you need to multiply/divide.

Don't use bitshift to multiply or vice-versa -- it just obfuscates your code and usually does not provide any practical performance boost. In fact in some cases it can actually hurt performance.
Last edited on
Nope you are wrong.


Division by powers of 2 is faster with unsigned int, because it can be optimized into a single shift instruction. With signed int, it usually requires more machine instructions, because division rounds towards zero, but shifting to the right rounds down. Thus when faced with performing a division by 2N on a signed integer, the compiler has no choice other than to invoke a signed divide routine rather than a simple shift operation.
Division by powers of 2 is faster with unsigned int, because it can be optimized into a single shift instruction. With signed int, it usually requires more machine instructions, because division rounds towards zero, but shifting to the right rounds down.


Yes I agree with this. Though this is not what you said originally -- you said shifting was faster with unsigned -- which isn't true.
Sorry, I shall reformat my question a bit :

What is the best way to learn x86 architechture, namely inline assembly to be used for various compilers like GNU C compiler in my case.

EDIT: The Original question was actually when should I shift n bits on unsigned binary number instead of signed binary number, because I see many of the implementations
of machine instruction to shift with unsigned operands.

http://en.wikipedia.org/wiki/Find_first_set

Can you give me example of __builtin_ctz and how to use it?
Last edited on
Topic archived. No new replies allowed.