2

I understand that shifting means moving each bit to the left or right respectively,but when I try to shift 0x30 by 4 positions to the left I get 0x300 or 00110000000 in binary.(my desired output would be 0000 0000 ).Why does it behave this way? My code in C:

int main(void){
 unsigned int a;
 scanf("%x",&a);
 printf("%b",a<<4);
}

Input:30 Output:300 Expected output:0 Edit:I would expect this output if I use more than 1 byte for my assigned variable ,but unsigned int is exactly 1 byte and 0x300 is 12 bits.

  • I'm not sure where you get that `unsigned int` is exactly 1 byte. It isn't. It's at least 2 bytes and almost always 4. You can check with `sizeof(unsigned int)` – Daniel H Nov 21 '18 at 09:16
  • int is 32-bit, not 8bit. – svtag Nov 21 '18 at 09:17
  • "unsigned int is exactly 1 byte": that seems highly unlikely; your premise might be wrong here. What is your platform (OS, hardware) that you are running on? See also https://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4-bytes – 9769953 Nov 21 '18 at 09:17
  • 4
    If you want 1byte memory, better to use `uint8_t` or `int8_t` data type, it is defined in header `stdint.h`. – svtag Nov 21 '18 at 09:18
  • @DanielH Actually, it *can* be... Once worked on a 16-bit DSP with CHAR_BIT == 16 and sizeof(int) == sizeof(char) == 1... – Aconcagua Nov 21 '18 at 09:28
  • 1
    @svtag not always, don't make that assumption. – Qix - MONICA WAS MISTREATED Nov 21 '18 at 09:32
  • @Aconcagua I thought of that, but decided it wasn't worth mentioning here. I don't know standard DSP terminology, but from outside that area you could at least make an argument that it has a two-byte `char` and the word "byte" has specifically come to mean eight bits. To be more precise, `unsigned int` must be at least 16 bits (assuming the underlying storage mechanism is even binary; the actual definition is in terms of maximum value) and is usually 32 on modern systems. – Daniel H Nov 21 '18 at 09:41
  • @DanielH It's a matter of definition: "byte" == 8-bit vs. "byte" == smallest addressable data unit (which on most systems is the same...). By the way: Standard does not speak of bits at all, only that unsigned int has to be capable to hold at least the values from 0 up to 65535. If you somehow happened to manage to pack these into less than 16 bit, it would still be valid... – Aconcagua Nov 21 '18 at 09:49
  • @Aconcagua I said the standard was defined in terms of maximum value. If you use a binary computer you can't squeeze that many values into less than 16 bits, but if your computer is decimal instead, you can use 5 digits instead. Although now that I think about it, other parts of the standard do depend on individual bits, so that might not be possible. – Daniel H Nov 21 '18 at 10:01

2 Answers2

3

a is an int which is (usually) 32 bits long.

The value of 0x300 is expected for an int.

Even if you use a uint8_t for a you need to typecast the result back to uint8_t to see the expected result.

int main(void){
  uint8_t a;
  scanf("%hhx",&a);  // you need hhx to read unsigned char (C99 only) 
  printf("%hhx",(uint8_t) (a<<4));
}

For your information, if you do not typecast, the value a<<4 will be promoted to int and 0x300 will be displayed

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • Originally I used uint8 in my program thinking that it's the same as uint8_t.I guess that was my first mistake. –  Nov 21 '18 at 09:26
  • 1
    it depends, in some systems/projects `uint8` is typecast as `uint8_t` and used. But the C standard is `uint8_t` – Rishikesh Raje Nov 21 '18 at 09:31
0

0x30 is hex and 0000000000110000 in binary. If you shift 4 bits, you get your result 0x300 or 0000001100000000.

To respond to your edit, unsigned int does not take 1 byte. It takes the same number of bytes as an int does. Which in your case it is probably 4 bytes or 32 bits.

The reason the number is shown with 2 or 3 hex digits, its because the 0-s in the beginning are not printed. But the number is in fact 0030(hex) .

Anyway, you can check the size using sizeof.

EDIT: If you want to take 1 byte, see Rishikesh answer.

kkica
  • 4,034
  • 1
  • 20
  • 40