Documente Academic
Documente Profesional
Documente Cultură
W IP RO CO
Version: <1.3> Date: < 23/10/2006>
Operations
FI N D TI EN
By FCG Team
AL
typedef struct { char name[30]; int empno; int salary; } EmpRec, * EmpRecPtr;
W IP
printf(Sizes: int=%d, char=%d, EmpRec=%d, EmpRecPtr=%d\n, sizeof(x), sizeof(y), sizeof(abc), sizeof(empr)); printf(Address: &x=%p, &y=%p, &z=%p, &abc=%p, &empr=%p\n, &x, &y, &z, &abc, &empr); printf("Address: &abc.name=%p, &abc.empno=%p, &abc.salary=%p\n", &abc.name, &abc.empno, &abc.salary); }
Wipro confidential 2
RO CO FI N D TI EN AL
typedef struct { char name[30]; int empno; int salary; } EmpRec, * EmpRecPtr;
Sizes: int=4, char=1, EmpRec=40, EmpRecPtr=4 Address: &x=0xbffffb3c, &y=0xbffffb3b, &z=0xbffffb34, &abc=0xbffffb00, &empr=0xbffffafc Address: &abc.name=0xbffffb00, &abc.empno=0xbffffb20, &abc.salary=0xbffffb24
W IP
printf(Sizes: int=%d, char=%d, EmpRec=%d, EmpRecPtr=%d\n, sizeof(x), sizeof(y), sizeof(abc), sizeof(empr)); printf(Address: &x=%p, &y=%p, &z=%p, &abc=%p, &empr=%p\n, &x, &y, &z, &abc, &empr); printf("Address: &abc.name=%p, &abc.empno=%p, &abc.salary=%p\n", &abc.name, &abc.empno, &abc.salary);}
Wipro confidential 3
RO CO FI N D TI EN AL
Memory view
&(abc.salary)
typedef struct { char name[30]; int empno; int salary; } EmpRec, *EmpRecPtr; main() { int x = 1; char y = 2; int z = 3; EmpRec abc; EmpRecPtr empr = &abc; printf("Sizes: int=%d, char=%d, EmpRec=%d, EmpRecPtr=%d\n", sizeof(x), sizeof(y), sizeof(abc), sizeof(empr)); printf("Address: &x=%p, &y=%p, &z=%p, &abc=%p, &empr=%p\n", &x, &y, &z, &abc, &empr); printf("Address: &abc.name=%p, &abc.empno=%p, &abc.salary=%p\n", &abc.name, &abc.empno, &abc.salary); }
&(abc.empno)
&(abc.name) b00
| | | | | | | | | | | | | | | | | | | | | p | | | p | p | p
Lower Memory
| |
| |
b1f b27
Higher Memory
W IP RO
&z
| | p | p | p | p | p 3 p 1 | | | | p 0 p 0
p p
CO
&x
FI N
| p | p | 0 | 0 | p | 2 | 0 | 0
TI EN
AL
&y
Wipro confidential
Memory address of a variable will align with their size. In some architectures, unaligned access can result in SIGBUS.
This is applicable for global variables, local variables, arguments passed to function and structure/union as well. To ensure address is correctly aligned, padding is done. Keep this in mind:
W IP RO
Assembly/C intermixing Suitable mask & shifting may needed Bit fields doesnt have such alignment requirements
Compiler generates code with masks & shifts to handle this.
CO FI N D TI EN AL
Wipro confidential 5
The registers can be accessed as my_hw_device->cntl1, my_hw_device->err and so on. Simplifies access to device registers, useful to device driver writers, OS developers.
W IP RO CO FI N D TI EN AL
Wipro confidential 7
Register sequence: Two 32 bit Registers(CNTL1, CNTL2, in that order) Three 8 bit Registers(STAT1, STAT2, STAT3, in that order) One 32 bit register CNTL3 One 8 bit register ERR Bad structure that can cause trouble: typedef struct hw_device { unsigned int cntl1; unsigned int cntl2; unsigned char stat1; unsigned char stat2; unsigned char stat3; unsigned int cntl3; unsigned char err; } MYDEVICE, *MYDEVICE_PTR;
W IP RO CO FI N D
TI EN AL
Wipro confidential 8
Bit Manipulations
main() { int x = -10; printf(%d\n, ~x+1); } main() { unsigned int x = 5; while(--x >= 0) { printf(Hello World\n); } }
W IP RO CO FI N D TI EN AL
Wipro confidential 9
Bit Manipulations
Output: 10 ~x represents ones compliment of x. ~x+1 represents twos compliment of x. (i.e negative of x)
main() { HelloWorld is printed infinitely. unsigned int x = 5; X becomes MAXINT when it is decremented while it is having a value of 0. while(--x >= 0) { printf(Hello World\n); } }
W IP RO CO FI N D TI EN AL
Wipro confidential
10
Output: 0xC0000000 Signed number, sign extension happens for right shift. (Arithmetic shift)
main() { unsigned int x = 0x80000000; printf(%x\n, x >> 1); Output: 0x40000000 } Unsigned number, sign extension
doesnt happen for right shift. (Logical shift)
W IP RO CO FI N D TI EN AL
Wipro confidential
11
Expression
1 2 3 4 5 6 7 8 9 X&1 X << 1 X >> 1 ~X+1 X ^ allones X >> 31 Mask = X >> 31 Result = (~Mask & X) | (Mask & (-X)) Mask = (X-Y) >> 31 Result = (Mask & X) | (~Mask & Y) Mask = (X-Y) >> 31 Result = (Mask & Y) | (~Mask & X)
Purpose
Check whether number is odd or even Multiply by 2 Divide by 2 Negative the number Bit toggling (equivalent to ~) To find out the sign of the number of X ABS(X) without using comparisions Min(X, Y) without using comparisions Max(X, Y) without using comparisions
Remarks
All numbers are interms of 0s and 1s and hence it is sufficient check the last bit
Result is 0 or -1 based on the sign. Mask contains all zeros if X is positive and all ones if X is negative. Mask contains all zeros if Y is less than or equal to X and all ones if X is less than Y. Mask contains all zeros if X is greater than or equal to Y and all ones if Y is greater than X.
IP
RO CO FI N D TI EN AL
Wipro confidential 12
Exercise 1: Power of 2.
Come up with an handy expression that can be used to detect whether the given number is a power of 2. Clue: All number that is a power of 2 will have just one bit set.
Examples:
1 00000001 2 00000010 4 00000100 8 00001000 16- 00010000 32 -00100000
W IP RO CO FI N D TI EN AL
Wipro confidential 13
Thank you.
Information contained and transmitted by this presentation is proprietary to Wipro Limited and is intended for use only by the individual or entity to which it is addressed, and contains information that is privileged, confidential or exempt from disclosure under applicable law.
W IP RO CO FI N D TI EN AL
Wipro confidential 14