Professional Documents
Culture Documents
018 - Bits, Characters and Strings
018 - Bits, Characters and Strings
18.1 Introduction
• Structure definition
struct Card {
char *face;
char *suit;
};
– Keyword struct
– Card is structure name
• Used to declare variable of structure type
– Data/functions declared within braces
• Structure members need unique names
• Structure cannot contain instance of itself, only a pointer
– Definition does not reserve memory
– Definition ends with semicolon
• Declaration
– Declared like other variables: use structure type
• Card oneCard, deck[ 52 ], *cPtr;
– Can declare variables when define structure
struct Card {
char *face;
char *suit;
} oneCard, deck[ 52 ], *cPtr;
• Structure operations
– Assignment to a structure of same type
– Taking address (&)
– Accessing members (oneCard.face)
– Using sizeof
• Structure may not be in consecutive bytes of memory
• Byte-alignment (2 or 4 bytes) may cause "holes"
Byte 0 1 2 3
18.5 typedef
• Keyword typedef
– Makes synonyms (aliases) for previously defined data types
• Does not create new type, only an alias
– Creates shorter type names
• Example
– typedef Card *CardPtr;
– Defines new type name CardPtr as synonym for type
Card *
• CardPtr myCardPtr;
• Card * myCardPtr;
• Bit operators
– Many are overloaded
– & (bitwise AND)
• 1 if both bits 1, 0 otherwise
– | (bitwise inclusive OR)
• 1 if either bit 1, 0 otherwise
– ^ (bitwise exclusive OR)
• 1 if exactly one bit is 1, 0 otherwise
• Alternatively: 1 if the bits are different
– ~ (bitwise one's complement)
• Flips 0 bits to 1, and vice versa
• Bit operators
– << (left shift)
• Moves all bits left by specified amount
• Fills from right with 0
• 1 << SHIFTAMOUNT
– >> (right shift with sign extension)
• Moves bits right by specified amount
• Fill from left can vary
• Next program
– Print values in their binary representation
– Example: unsigned integer 3
• 00000000 00000000 00000000 00000011
• (For a machine with 4-byte integers)
• Computer stores number in this form
• Using masks
– Integer value with specific bits set to 1
– Used to hide some bits while selecting others
• Use with AND
• Mask example
– Suppose we want to see leftmost bit of a number
– AND with mask
• 10000000 00000000 00000000 00000000 (mask)
• 10010101 10110000 10101100 00011000 (number)
– If leftmost bit of number 1
• Bitwise AND will be nonzero (true)
– Leftmost bit of result will be 1
• All other bits are "masked off" (ANDed with 0)
– If leftmost bit of number 0
• Bitwise AND will be 0 (false)
• Upcoming examples
– Demo operators
– & (AND)
•x & y
– | (OR)
•x | y
– ^ (Exclusive OR)
•x ^ y
– ~ (Complement)
• ~x
– << and >> (Left shift and right shift)
• Bit field
– Member of structure whose size (in bits) has been specified
– Enables better memory utilization
– Must be declared int or unsigned
– Example
Struct BitCard {
unsigned face : 4;
unsigned suit : 2;
unsigned color : 1;
};
– Declare with name : width
• Bit width must be an integer
• Other notes
– Bit fields are not arrays of bits (cannot use [])
– Cannot take address of bit fields
– Use unnamed bit fields to pad structure
Struct Example {
unsigned a : 13;
unsigned : 3;
unsigned b : 4;
};
– Use unnamed, zero-width fields to align to boundary
Struct Example {
unsigned a : 13;
unsigned : 0;
unsigned b : 4;
};
• Automatically aligns b to next boundary
• Upcoming example
– isalpha( int c )
• (All character functions take int argument)
• Returns true if c is a letter (A-Z, a-z)
• Returns false otherwise
– isdigit
• Returns true if digit (0-9)
– isalnum
• Returns true if letter or digit (A-Z, a-z, 0-9)
– isxdigit
• Returns true if hexadecimal digit (A-F, a-f, 0-9)
fig18_17.cpp
According to isalpha:
output (1 of 1)
A is a letter
b is a letter
& is not a letter
4 is not a letter
According to isalnum:
A is a digit or a letter
8 is a digit or a letter
# is not a digit or a letter
According to isxdigit:
F is a hexadecimal digit
J is not a hexadecimal digit
7 is a hexadecimal digit
$ is not a hexadecimal digit
f is a hexadecimal digit
• Upcoming example
– islower
• Returns true if lowercase letter (a-z)
– isupper
• Returns true if uppercase letter (A-Z)
– tolower
• If passed uppercase letter, returns lowercase letter
– A to a
• Otherwise, returns original argument
– toupper
• As above, but turns lowercase letter to uppercase
– a to A
u converted to uppercase is U
7 converted to uppercase is 7
$ converted to uppercase is $
L converted to lowercase is l
• Upcoming example
– isspace
• Returns true if space ' ', form feed '\f', newline '\n',
carriage return '\r', horizontal tab '\t', vertical tab '\v'
– iscntrl
• Returns true if control character, such as tabs, form feed,
alert ('\a'), backspace('\b'), carriage return, newline
– ispunct
• Returns true if printing character other than space, digit, or
letter
• $ # ( ) [ ] { } ; : %, etc.
• Upcoming example
– isprint
• Returns true if character can be displayed (including space)
– isgraph
• Returns true if character can be displayed, not including
space
According to ispunct:
; is a punctuation character
Y is not a punctuation character
# is a punctuation character
According to isprint:
$ is a printing character
Alert is not a printing character
According to isgraph:
Q is a printing character other than a space
Space is not a printing character other than a space
• Functions
– double atof( const char *nPtr )
• Converts string to floating point number (double)
• Returns 0 if cannot be converted
– int atoi( const char *nPtr )
• Converts string to integer
• Returns 0 if cannot be converted
– long atol( const char *nPtr )
• Converts string to long integer
• If int and long both 4-bytes, then atoi and atol identical
• Functions
– double strtod( const char *nPtr, char
**endPtr )
• Converts first argument to double, returns that value
• Sets second argument to location of first character after
converted portion of string
• strtod("123.4this is a test", &stringPtr);
– Returns 123.4
– stringPtr points to "this is a test"
• Functions
– long strtol( const char *nPtr, char
**endPtr, int base )
• Converts first argument to long, returns that value
• Sets second argument to location of first character after
converted portion of string
– If NULL, remainder of string ignored
• Third argument is base of value being converted
– Any number 2 - 36
– 0 specifies octal, decimal, or hexadecimal
– long strtoul
• As above, with unsigned long
memcmp(s1, s2, 4) = 0
fig18_37.cpp
memcmp(s1, s2, 7) = -1
output (1 of 1)
memcmp(s2, s1, 7) = 1
string1 = BBBBBBBBBBBBBB
string1 after memset = bbbbbbbBBBBBBB