0

Here's my problem. I have the following structure defined

struct idt_reg{
  unsigned short limit;
  unsigned int base;
}__attribute__((packed));

In my code I do the following assignment

unsigned short num_ids = idtr.limit >> 3;

Now upon execution, when the value stored in idtr.limit is equal to 2047(0x7FF), what I expected to happen was a right shift of 3 bits (divide by 8) and get the value of 255 written to num_ids. Instead the value of num_ids is always 0.

Any help I would really appreciate.

Shweta
  • 5,198
  • 11
  • 44
  • 58
Micky
  • 63
  • 4
  • 2
    "what I expected to happen was a right shift of 3 bytes (divide by 8)" Nope, a right shift of 3 __bits__. – orlp Jun 07 '11 at 07:05
  • 4
    If right-shifted by 3, it would be 255 (0xFF). ;) – Jeff Mercado Jun 07 '11 at 07:06
  • 1
    So could you show a more complete snippet. How you set `idtr`, how you determine that `num_ids` is `0`. Did you print it? See it in a debugger? etc. – Jeff Mercado Jun 07 '11 at 07:08
  • Expanding the missing parts, it works here as expected (255 and not 256). – AProgrammer Jun 07 '11 at 07:14
  • 1
    Have you printed out the value of idtr.limit and are sure it has the value 2047 , or are you just assuming it should have the value 2047 ? – nos Jun 07 '11 at 08:14
  • I am supremely sorry guys. I seem to have made that shift before assigning a value to idtr.limit. My utmost apologies to you all. It's a terrible lack of concentration on my part. Sorry for the inconvenience. – Micky Jun 07 '11 at 14:19

1 Answers1

1

This:

#include <stdio.h>

struct idt_reg{
  unsigned short limit;
  unsigned int base;
}__attribute__((packed));

int main() {
    struct idt_reg idtr;
    unsigned short num_ids;
    idtr.limit = 2047;
    num_ids = idtr.limit >> 3;
    printf( "%d\n", num_ids );
}

prints 255.