The Open Source Swiss Army Knife

/programmingToolBox/comp_orgI/
/programmingToolBox/comp_orgI/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /programmingToolBox   /comp_orgI 
Permalink: data_tipz.htm
Title: microsoft and unix specific and ISO c data types (int, size_t...
article options : please login   |  print view

bits and bytes

definitions

the signed values use the high-most bit for a sign, so numbers are limited to 127 to -127 for signed and to 255 for unsigned

signed integers are not always reproducible straight from 010101 by two's conversion, but may also be stored in (as) twos complement, ones complement, sign-magnitude. This can lead to portability problems as on different systems the unsigned integers are represented differently.

it is usually best therefore to use the unsigned type to perform integer conversions

negative numbers representation
sign-magnitude
easiest to understand, -16 and 16 are same in binary, but the first bit is a "1" when it's -16
ones complement
simply reverses the bits to represent negative numbers (can tell diff between +16 and -16 by looking at the first bit)
twos complement
the most widely used
is not a simple oring of values: reverses all the bits and then adds one

if the programmer performs bitwise operations on numbers, results may not be as expected when signed types are used

representations

how do these look in declarations?
unsigned msb
in this example, represents the most significant byte in a network address
get this with msb = * (unsigned char *) &adr_inet.sin_addr.s_addr
unsigned char

operators

you know of course the bitwise operators are different from the logical operators...

bit arrays are bit arrays, not of predefined types, on which bit operations will be performed

memset operates on bit arrays, zeroing out all the bits





Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top