• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Conversion issue

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am having a doubt in the questions involving hexadecimal numbers.
i had this question in a mock exam

int ab = 0xFFFFFFFA;
int invertedA = ~ab;

Now how am i supposed to find value of ab which is -6,i mean am is supposed to write
A * 16^0 + F * 16^1 + F * 16^2 and so on ???
i don't think this is the proper way,is there no easier way ? how will i get -6 as the answer.
plz. solve my doubt,
thanks in advance.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, I believe the answer is 5, not -6.
To see how to do the inversion easily, picture what this number looks like in binary. It is:
11111111 11111111 11111111 11111010
Inverting means 1 becomes 0 and vice versa:
00000000 00000000 00000000 00000101
or 101 for short, which is to say 5.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Lovelace:

11111111 11111111 11111111 11111010
Inverting means 1 becomes 0 and vice versa:
00000000 00000000 00000000 00000101
or 101 for short, which is to say 5.


Don't forget!!! Two's complement means you need to invert the number and then add 1!
So -->

11111111 11111111 11111111 11111010
00000000 00000000 00000000 00000101
+ 1
-----------------------------------
00000000 00000000 00000000 00000110
== 6
So that means it really is -6
But I agree -- the easiest way I've found to convert hex digits is first to convert it to binary then two's complement it (if its negative) and then figure out the decimal representation.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ab=0xFFFFFFFA
in hexadecimal A=10, B=11, C=12, D=13, E=14, F=15
in binary 10,11,12,13,14,15 are 1010, 1011, 1100, 1101, 1110, 1111 resp
so ab = 1111 1111 1111 1111 1111 1111 1111 1010
~ab = 0000 0000 0000 0000 0000 0000 0000 0101 + 1 = 110 = -6

Can anybody tell me how to convert these?
byte a = (byte)127;
byte b = (byte)128;
byte c = (byte)255;
byte d = (byte)256;
//......
System.out.print(a + " " + b + " " + c + " " + d);
Ans: a = 127, b = -128, c = -1, d = 0
Thanks.
[ November 19, 2003: Message edited by: dimple kaushik ]
 
Vishy Karl
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all
Jessica the number given ab is itself -6 according to 2's complement .
then after the operation
int invertedA = ~ab;
it comes to sign change and answer as 5
Thanks all for your answers,
happy preparation,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic