Lower 8-bits of integer

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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

int lower = full & 0xFF ;
char ch = (char)( theInt & 0xFF ) ;
To get the 8 bits above that, you'd do:

char ch = (char)( ( theInt >> 8 ) & 0xFF ) ;
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

these examples assume that "full" or "theInt" is you int variable :-)
robert_83Author Commented:
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 '?'

"&" does a bitwise AND on the integer value...

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


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...

Ahhh...using your example:

1111101000000 (8000) AND
0000011111111 (255 -- 0xFF)
0000001000000 (64)
robert_83Author Commented:
thanks for the reply, clear and concise.

I have a text file containing "r".

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

Why does it output ?.

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...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.