Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Lower 8-bits of integer

Posted on 2003-11-24
9
Medium Priority
?
382 Views
Last Modified: 2010-03-31
Given an integer of max length 16-bits how does one read the lower 8-bits, or is this not really a question.

I read that a java method converts the lower 8-bits of its integer param and converts it to the ASCII equiv.

Cheers/
0
Comment
Question by:robert_83
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 2
9 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9810113
int lower = full & 0xFF ;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9810114
char ch = (char)( theInt & 0xFF ) ;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9810125
To get the 8 bits above that, you'd do:

char ch = (char)( ( theInt >> 8 ) & 0xFF ) ;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:TimYates
ID: 9810127
these examples assume that "full" or "theInt" is you int variable :-)
0
 

Author Comment

by:robert_83
ID: 9810299
Could you briefly explain the theory of suppose i supply 8000, how to get the lower 8-bits.

This question originated from using DataInputStream readChar, then output the char. No matter what character it read from the text file it always outputted '?'

0
 
LVL 35

Expert Comment

by:TimYates
ID: 9810373
"&" does a bitwise AND on the integer value...

0xFF is 11111111 in binary, so say you have the number 345, in binary, that is:

101011001

so, if we AND it wil 0xFF:

101011001 (345) AND
011111111 (255 -- padded on the left with a zero)
-------------
001011001 (89)

Hope this explains it?

Let me know if you need more of an explanation...

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9810383
Ahhh...using your example:

1111101000000 (8000) AND
0000011111111 (255 -- 0xFF)
------------------
0000001000000 (64)
0
 

Author Comment

by:robert_83
ID: 9811003
thanks for the reply, clear and concise.

I have a text file containing "r".

dis = DataInputStream(new FileInputStream(file));
char c = dis.readChar()
System.out.println(c);
output = ?

Why does it output ?.

Cheers/
0
 
LVL 35

Accepted Solution

by:
TimYates earned 200 total points
ID: 9811302
If it's a text file, use a Reader:

BufferedReader br = new BufferedReader( new FileReader( file ) ) ;
char c = (char)br.read() ;
System.out.println( c ) ;

should work...

if not, it is probably an encoding problem...
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question