• 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

Bytes and Bits help

 
Greenhorn
Posts: 13
1
C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
So I am currently trying to solve

hex[(b>>4) & 0x0f] + hex[b & 0x0f]

and I have worked it out until hex[15] + hex [1], But I dont know how to proceed from there...because, say the "hex" part wasnt there in the expression, wouldnt 15+1, considering they were decimals and we did not have to convert, be 16, which would be 0x10? D:

Then why does the console window give me an output of 0xf1..unless I am making a mistake in working it out...

Any help or contribution will be greatly appreciated. Thanks.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sarah,
Welcome to CodeRanch!

I'm moving this from the OCPJP forum to our Java in General forum since bit twiddling is no longer on the certification exam.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a java program you are executing?
Could you post the code (wrapped in code tags) and the program's output.
Add some comments describing what you don't understand.
 
Sarah Singh
Greenhorn
Posts: 13
1
C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Jeane

Hi Norm,

public class HexByte {

public static void main(String[] args) {

char hex [] = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f' };
byte b = (byte) 0xf1;

System.out.println("b = 0x" + hex[(b>>4) & 0x0f] + hex[b & 0x0f] );

}

}

I have worked out hex[(b>>4) & 0x0f] + hex[b & 0x0f] to be hex[15] + hex[1] but I have no idea how will hex[15] + hex[1] works out D:
 
Norm Radder
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the program's output?
Also please wrap the code in code tags.

how will hex[15] + hex[1] works out


Code a statement to print the result of the expression so you can see what the results are.
 
Sarah Singh
Greenhorn
Posts: 13
1
C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Norm The following is the code and I understand the question I asked now :P
The square brackets, meant array and hence the line hex[15] + hex[1] was referring to array index, hex[15] and hex[1] and the results were being added.
Thanks for looking into my problem \(^.^)/ Much Appreciated!

package Operators;


 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is your problem solved now?
 
Sarah Singh
Greenhorn
Posts: 13
1
C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Norm
Yes it is!
Thank you \(^.^)/
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic