• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

octal/hex/binary/unicode ?

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I could find questions to manipulate on 16/32 bits on binary or other representations. Assuming I knew the basics of all these and the bit operations done on these representations,
1) what other things should I get to know from exam pt. of view ?
2) I need to have a good refresh on conversions of all these representations in other formats, could anyone post a simple exam constituting all ?
3) I found questions interpreting the values of UNICODE, what do i need to study on these.
I am quite confused on these topics, Any help is appreciated !!!
thanks ~ Shalini
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Taurean -
I'm not sure I understood all of your question, but I'll take a stab at what I think you're asking:
- It's very important to understand two's complement
- It's very important to understand the shift operators
- It's very important to understand how hex (a lot), and octal (a little), are represented in binary
Hope that helps as a partial answer :roll:
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert I have a hard time understand those topics on your programmer book.
do you have any more info on how to master those 3 topics.

Thanks.

 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dev -
Sorry to hear you're still confused. Can you narrow it down a bit? Are you comfortable with binary? How about two's complement? Where exactly are we losing you big guy?
 
Ashok Paulraj
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert,
To start out with, where could I learn to convert a hex code to UNICODE. itz been asked in one MOCK exam and I just blinked !!...
~ Shalini
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Bert, could you also remind me of twos compliment?!
and also, in another thread, i have been to,
why?:

I thought the >>> operator made all bits in the left hand side 0's ( by whatever number used), but this doesn't look as if it is, or was i getting taught wrong??
Davy
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I think maybe some bartenders on this forum might have links to some other resources on these topics, but let's do a quick intro...
For the sake of these discussions let's let's talk about a byte or 8 bits: (What I'll say about a byte holds true for shorts, ints, and longs (the numeric primitives), as far as two's complement goes. )
For all of the numeric primitives, you can store positive AND negative numbers in the bits that represent the primitive. The leftmost bit for a numeric primitive is called the sign bit, if it's on the number is negative, if it's off, the number is positive.
So since a byte is 8 bits, the biggest positive number we can represent is:
0111 1111 which is 127 in binary. To store a bigger number, we'd have to use the leftmost bit, but that would turn the whole darned thing to negative. So you might think that
1000 0001 would be 129 (two more than the earlier example, but because the pesky left bit is on it's actually some negative number!
Two's complement is the approach that Java uses to store negative integers. The rule for two's complement is:
"flip all the bits then add 1"
You can do this in either direction, positive to negative or negative to positive, for instance:
0000 0101 is 5
1111 1010 all the bits flipped
1111 1011 after adding one, this is -5
now back again
1111 1011 -5
0000 0100 flipped all the bits
0000 0101 add 1, is 5, right back where we started !
So what if someone says what's 1010 1101 equal? Well let's find out
1010 1101 some negative number
0101 0010 flip the bits
0101 0011 add 1, and 64+16+2+1 = 83 so 1010 1101 = -83
A little more...
1000 0000 what's this negative number?
0111 1111 flip the bits...
1000 0000 add 1 = 128 so 1000 0000 is -128
1000 0011 what's this negative number?
0111 1100 flipped
0111 1101 is 64+32+16+8+4+1 = 125 so 1000 0011 is -125
1111 1111 what's this?
0000 0000 flipped
0000 0001 add 1, so 1111 1111 = -1 !
So 1000 0000 is a big negative number
and 1111 1111 is a small negative number
Does this explain how the range of a byte is -128 to 127 ?
Ok, that's two's complement, wha't next?
[ January 17, 2004: Message edited by: Bert Bates ]
[ January 17, 2004: Message edited by: Bert Bates ]
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good explanaition Bert,
But in my above post,

would you do it like this:
-128 = 1111 1111
bitshift it to get 0011 1111
flip the bits and add 1 to get 1011 1110?? but this would give an answer of -66
I think I am getting this wrong!
Davy
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,
Ok, you can probably tell by now I'm not going to just give you the answer directly , I'm going to turn it into a "learning experience" :roll:
First off, are you sure that -128 = 1111 1111 ? double check your two's complement logic first!
let me know!
Bert
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well if i take the -128 and make them into bits i would get 1111 1111 but when i flip the bits and add 1 i would get an integer that would give me:
11111111 11111111 11111110 00000001 have i got this correct so far?
Davy
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah Davy,
Now I get it! Sorry!
To find out the bits for a negative number, do this:
Let's say we want to know how -32 looks in bits:
start with positive 32: 0010 0000
shift bits: 1101 1111
add 1: 1110 0000
So -32 = 1110 0000
now you get to try it with -128...
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah Davy,
Now I get it! Sorry!
To find out the bits for a negative number, do this:
Let's say we want to know how -32 looks in bits:
start with positive 32: 0010 0000
shift bits: 1101 1111
add 1: 1110 0000
So -32 = 1110 0000
now you get to try it with -128...
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so -128 would be:

1111 1111 flip the bits to get:
0000 0000 add 1 to get:
0000 0001
so this gives 1, but because it was a negative number in the first place make the positive a negative, so 1 now become -1
Davy
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Davy -
We're getting closer...
But I don't agree that 1111 1111 = 128...
Forget about two's complement for now, what's positive 128 in binary?
Bert
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey bert this is how i normally find the binary, I may be wrong:
128 64 32 16 8 4 2 1
1 0 0 0 0 0 0 0
So i hope this gives positive 128, ah sorry i read you question wrong.
but if this is positive 128, does the byte have 16 bits? e.g.
00000000 10000000
Davy
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
above should have looked like this:

Davy
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Davy,
Yes!!! You got it!!
Let's just talk about an 8 bit byte for now,
128 = 1000 0000 in binary (notice how i split them into 4 bit chunks
So now, take your two's complement formaula, starting with 1000 0000
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
twos compliment would give me:
in 8 bits:
1000 0000
0111 1111 add 1 would give:
1000 0000 ?
Have i understood you correctly?
Davy
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Davy,
Yes! 128 is the weirdest one, because as you can now see, you can't really do 128 in a byte because 1000 0000 is actually -128! So the range for a byte is -128 - 127
Now, can you proceed with your original example?
Bert
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Davy,
Yes! 128 is the weirdest one, because as you can now see, you can't really do 128 in a byte because 1000 0000 is actually -128! So the range for a byte is -128 - 127
Now, can you proceed with your original example?
Bert
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one quick question about conversions if you dont mind.
I understand that conversion from each is important, but should we be expecting questions that expect us to convert from octal to hex before applying shift operations. It seems it would take sometime to complete the test if several were given...
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Bert,
(i just thought of Ernie saying that from seseme street):
I get the two's compliment now, but when would I do the bitshifting to get the correct answer, before or after the conversion?
so because byte is only 8 bits and gives me a range of -128 - 127,
then if i go beyond this range the answer would get promoted to an int?
and if the value of this int is assigned to a byte without casting then it would generate a comiler error?
Davy
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Bert,
I think I have it now,
if i have a byte of 1000 0000 and I want to bit shift it, then the byte gets promoted to int of 32 bits.
So -128 would be:

in the int stakes, but i was looking for the byte so we cut off the 24 left most bits.
to get 1110 0000 to give me -32.
But what you were saying above was

Have I got it now???
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Bert,
I think I have it now,
if i have a byte of 1000 0000 and I want to bit shift it, then the byte gets promoted to int of 32 bits.
So -128 would be:

in the int stakes, but i was looking for the byte so we cut off the 24 left most bits.
to get 1110 0000 to give me -32.
But what you were saying above was

Have I got it now???
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why are the posts getting duplicated?
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Davy - YES!
I think you've got it!
The only reason we do two's complement, is that it's an easy way to figure out what the bits really look like for a negative number. Once we know the bit pattern of the negative number then we can do the shift.
And you're also right that because in this case we started with a byte, it got promoted to an int. After the shift, we have to cast the int back to a byte to get -32.
Hooray!
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers for the help bert, much appreciated.
Davy
 
It's just a flesh wound! Or a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic