Question

Consider the following code: int i = 0x56789a; char c = (char) i; int j =...

Consider the following code:

int i = 0x56789a;

char c = (char) i;

int j = c;

What is the hex value of c? (0x9a)

What is the hex value of j? (0xffffff9a)

I know the answer but I don't know how to get to that answer.

Can someone explain this to me, please? Thanks!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
int i = 0x56789a;
char c = (char) i;

char can only store a single byte.
and when you cast an integer to a char. then copy right most byte(0x9a) from i into char c.
so, c becomes (0x9a)

int j = c;

Converting 9A to binary
9 => 1001
A => 1010
So, in binary 9A is 10011010

we know that size of int is 32 bits.
when we copy a char(8 bits) into int, then copy the left most bit of c into remaining bits of int

so, int in binary is 11111111 11111111 11111111 10011010
now, convert this to hexadecimal

Hexadecimal     Binary
    0           0000
    1           0001
    2           0010
    3           0011
    4           0100
    5           0101
    6           0110
    7           0111
    8           1000
    9           1001
    A           1010
    B           1011
    C           1100
    D           1101
    E           1110
    F           1111
Use this table to convert from binary to hexadecimal
Converting 11111111111111111111111110011010 to hexadecimal
1111 => F
1111 => F
1111 => F
1111 => F
1111 => F
1111 => F
1001 => 9
1010 => A
So, in hexadecimal 11111111111111111111111110011010 is 0xFFFFFF9A

that's why value of i contains value of 0xffffff9a
Add a comment
Know the answer?
Add Answer to:
Consider the following code: int i = 0x56789a; char c = (char) i; int j =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++: what is i and j. IMPORTANT I know it's 3 and 3 but can someone...

    C++: what is i and j. IMPORTANT I know it's 3 and 3 but can someone please explain how we can get to that conclusion by just reading the code, can it be explained to me because when i try to do it I get a different answer for i and j I got 1 and 4. #include <iostream> using namespace std; int main() { int i = 1; int j = 0; if (i++ == 1) { j =...

  • Consider the following code C++ like program: int i, j, arr[5]; //arr is an array starting...

    Consider the following code C++ like program: int i, j, arr[5]; //arr is an array starting at index 0 void exchange(int x, int y) { int temp:= x; x:= y; y:= temp; } main(){ for (j = 0; j < 5; j++) arr[j]:= j; i:= 1; exchange(i, arr[i+1]); output(i, arr[2]); //print i and arr[2] } What is the output of the code if both parameters in function swapping are passed by: a- value? b- reference? c- value-result?

  • The following code snippet is for C++ int selection_Sort(int A[ ], int n) { int I,...

    The following code snippet is for C++ int selection_Sort(int A[ ], int n) { int I, j, small, temp; for( i = 0; i < n-1; i++) { small = i; for(j = i + 1; j < n; j++) { if ( A[ j ] < A[ small ] small = j; } temp = A [ i ]; A[ i ] = A[small]; A[small] = temp; } } Please explain in rich detail the logic behind every execution...

  • Consider the following C++ code segment: for (int i = 0; i <n; ++i) { for...

    Consider the following C++ code segment: for (int i = 0; i <n; ++i) { for (int j = 0; j <m; ++j) if (i != j) cout << "0"; else cout << "1"; } } Which of the options below gives the correct output if the value of nis 2and the value of mis 3? 1. 100010 2. 011101 3. 100100 4. 010001

  • I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int...

    I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int main(int argc,char* argv[]) {      if(argc > 1)      {            char* commandName;            for(int i=2;i            {                   forkChild = fork();                   signal(SIGINT,sighandler);                   if(forkChild == 0)                   {                          execlp(commandName,commandName,argv[i],NULL);                          exit(0);                   }                   else                   {                          wait(NULL);                   }       } My problem is that I would like to kill the child with ^C but leave the parent running....

  • i need it quick teacher 28. Consider the code int k; char c = "ABCDEFG"; char...

    i need it quick teacher 28. Consider the code int k; char c = "ABCDEFG"; char *cp: for(cp = C+2; cp >= c.) printf("%c", Missing 1); printf("\n"); for(cp = (+3; cp <= (+6;) printf("%c". _Missing_2 ): However, part of the code is missing (indicated by _). The code is supposed to give the output CBA EFG What can the missing parts be? (4 Points) Missing 1-cp Missing 2: *+-cp Missiasto-- Missing 2*+-ep Nasil. Missing 2: *cp) + ĐI II 1...

  • Consider the following segment of C-code: int j, n; j = 1; while (j <= n)...

    Consider the following segment of C-code: int j, n; j = 1; while (j <= n) j = j*2; Group of answer choices FLOOR(logn) + 2 CEIL(logn) n CEIL(logn) + 2

  • #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='...

    #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='z';      pc=&(c[8]);      pk=&(k[0]);      for (i=0; i<9; i++)     {           *pc=a-(char)i;           pc--;           *pk=(int)a-i;           pk++;           }                     return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...

  • Consider the following C program. It calculates the remainder when i is divided by j. For...

    Consider the following C program. It calculates the remainder when i is divided by j. For example, function call remainder(9,4) returns value 1. int remainder(int i, int j){ if(i<j) return i; else return remainder(i-j,j); } Please translate the above C code into MIPS.

  • #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1;...

    #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function  

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT