Conversation started Apr 13, 2020 at 11:08.
Apr 13, 2020 11:08
Do you know what hexadecimal notation is?
just that address are represented by them
i don't know in detail
A byte has eight bits, and this neatly splits into two 4 bit chunks (these are often called nibbles)
4 bits can store a number from 0 to 15.
In hexadecimal we write the value of the nibble as 0-9 then A-F.
i.e. if the value in the four bits is in the range 0 to 9 we just write it as 0 to 9. If the value is 10 we write it as A, 11 we write as B, and so on up to 15 we write as F.
ah okay
Apr 13, 2020 11:13
So any byte can be written in hex as two characters 0-9, A-F
ah now it makes sense
The reason we do this is that it makes it easy to see what the byte is because hex is easily converted to binary.
So for example a space character has the ascii value 32, and in binary that is 00100000
If we split this into two we get:
0010 0000
  2    0
Apr 13, 2020 11:16
Where I've written the corresponding hex digit below each nibble.
That's why 32 decimal is 0x20 hex.
(The 0x prefix just means hex format - it isn't part of the number)
ah okay makes sense now
So anyway, if the four bytes start out as 0x00000000 and we set c[0] = ' ' (32) then the four bytes become 0x20000000.
And if we set c[1] to c[3] to space as well then the four bytes become 0x20202020
OK so far?
If we now use those four bytes as an int then we get the int with the value 0x20202020 and that is the number 538976288 that your program printed.
Apr 13, 2020 11:21
You're probably wondering why on earth anyone would do this, and that's a good question.
yeah.This seems kinda complicated enough
I think there are very few good uses for unions.
Possibly when programming microcontrollers, but very rarely for programs on PCs.
But I guess it's something you need to know to pass the C exams.
// Unions
#include <stdio.h>
union xyz
{
  int x;
  char y;
  struct x1{
	  int x2;
	  float y2;
  }xx1;
};
int main()
{
 union xyz xy;
  xy.x=321;
  printf("x = %d\t y= %d",xy.x,xy.y);
  xy.y='Z';
  printf("x = %d\t y= %d",xy.x,xy.y);
}
can u explain this if u are free
it seems slightly different from the previous programme
Let me change the program slightly:
#include <stdio.h>

union xyz
{
  int x;
  char y;
  struct x1{
	  int x2;
	  float y2;
  }xx1;
};

int main()
{
 union xyz xy;
  xy.x=321;
  printf("x = %X\t y= %X\n",xy.x,xy.y);
  xy.y='Z';
  printf("x = %X\t y= %X\n",xy.x,xy.y);

  return 0;
}
I get:
D:\rhs\c>rat
x = 141  y= 41
x = 15A  y= 5A
yeah even i get the same
Apr 13, 2020 11:31
xy.x is an int, so when we set xy.x = 321 we set the first four bytes to 321, which is 0x141 hex. So the four bytes are now:
0x00000141
OK so far?
And xy.y is a char, so when you look at the value of xy.y you get just the first byte, which is 0x41. That's why when you print xy.y it prints 41.
Ah, there's something I need to make clear.
In C numbers are stored with the bytes in reverse order. So the number 312 is 0x141 in hex, but it's actually stored as the four bytes:
41 01 00 00
i.e. the bytes are stored in reverse order.
Apr 13, 2020 11:37
That's why when I say xy.y is the first byte it's the byte with the value 0x41
ah let me think over this
I'm not sure I'd worry too much about unions. You rarely use them in real life.
@JohnRennie Will u be back later
Yes, I'll be around later today and tomorrow as usual.
ok
xy.x is an int, so when we set xy.x = 321 we set the first four bytes to 321, which is 0x141 hex. So the four bytes are now:
can u tell how u got 141
Apr 13, 2020 11:55
0x141 is the same number as 312 decimal.
binary of 321 is:
101000001
if we take 8 bits from this
we get:
@Aladdin Split that into nibbles and you get:
0001 0100 0001
0100,0001
why extra 000
?
Because a nibble is always four bits.
I'm showing all four bits even though some are zero
won't this exceed 4 bytes
Apr 13, 2020 12:00
0001  0100  0001
  1     4     1
@Aladdin I'm not sure why you say that?
0x00000141
so it is:
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 0100 0001
No, it's just:
0000 0000 0000 0000 0000 0001 0100 0001
Eight nibbles = four bytes
oh i get it
In C numbers are stored with the bytes in reverse order. So the number 312 is 0x141 in hex, but it's actually stored as the four bytes:
41 01 00 00
reverse of 0000 0000 0000 0000 0000 0001 0100 0001
Yes, just to be awkward most processors reverse the order when they store ints.
I need to go now. 'll be around later.
 
Conversation ended Apr 13, 2020 at 12:07.