Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

integral literals? 0xff

 
Duncan Allen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am having difficulty understanding why the values represented in these statements are what they are
int b=0xff
int i= 0x7FFF
When I compile and run i get 255 and 2047.
Is there an easy way to understand what and how these numbers represent?
There was a great posting a little while ago explaining Octal and Hex numbers, and I am clear on their values.
I am scheduled to write the exam a week from today and would like to iron out these bugs in my brain. Thanks for your help.
p.s. Should I understand how to do these calculations. I see them occasionally on mock exams.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there you go:
Topic: converting decimals to binary
Topic: value & 0xff
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

int b=0xff
int i= 0x7FFF
When I compile and run i get 255 and 2047.

Val gave some great links, but 0x7FFF is not 2047, it's 32767.
Corey
 
Duncan Allen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx guys,
You are quite right Corey.
typo , one too many f's
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a simple little rule.
1. ALL integral literals are of type int, regardless of their radix (base notation).
Examples:
127 //int literal
456789 //int literal
016 //int literal represented in OCTAL notation
0xFF //int literal represented in HEXADECIMAL notation
0Xffabcd //int literal represented in HEX.
Rule #2:
2. All floating point literals are of type double, casts not withstanding (ie, yes you can include an F or f or (float) and then it won't be a double but I'm just talking about a plain undecorated floating-point literal value)
examples:
1. //double
.1 //double
0.1 //double
123.456 //Can you guess what this is?
789.12 //boolean.
NO THAT WAS A JOKE. IT'S ALSO A DOUBLE.
Hope this makes it crystal clear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic