J
jonathan
Hello all,
Can someone tell me: do bitwise operators only work on ints and
"above"? I was auditing some code for bad implicit casts, and one that
came up was of the form "char = char ^ char". Apparently the xor
promotes the types to int and then it was getting cast implicitly to
char again for the assignment. This doesn't make a whole lot of sense
to me... Is this just the compiler (g++ 4.3.2)? Or is this the way it
supposed to be in the C++ specification?
--Jonathan
Example:
#include <iostream>
int main(void) {
unsigned char a = 3, b = 2, c;
c = a ^ b; // I sorta get what this one does. But it "shouldn't"
imho
a ^= b; // I really don't have any idea why a promotion is
happening here
std::cout << c << std::endl;
return 0;
}
Output:
g++ -Wconversion test.cpp
test.cpp: In function ‘int main()’:
test.cpp:6: warning: conversion to ‘unsigned char’ from ‘int’ may
alter its value
test.cpp:7: warning: conversion to ‘unsigned char’ from ‘int’ may
alter its value
Can someone tell me: do bitwise operators only work on ints and
"above"? I was auditing some code for bad implicit casts, and one that
came up was of the form "char = char ^ char". Apparently the xor
promotes the types to int and then it was getting cast implicitly to
char again for the assignment. This doesn't make a whole lot of sense
to me... Is this just the compiler (g++ 4.3.2)? Or is this the way it
supposed to be in the C++ specification?
--Jonathan
Example:
#include <iostream>
int main(void) {
unsigned char a = 3, b = 2, c;
c = a ^ b; // I sorta get what this one does. But it "shouldn't"
imho
a ^= b; // I really don't have any idea why a promotion is
happening here
std::cout << c << std::endl;
return 0;
}
Output:
g++ -Wconversion test.cpp
test.cpp: In function ‘int main()’:
test.cpp:6: warning: conversion to ‘unsigned char’ from ‘int’ may
alter its value
test.cpp:7: warning: conversion to ‘unsigned char’ from ‘int’ may
alter its value