i**p 发帖数: 902 | 1 哪位大牛能解释一下ullong 1, ullong 2, ullong 4, ullong 5 的输出吗?
此程序在 Android (GB) emulator 上运行。
//刚刚发现,程序中的反斜杠都被mitbbs过滤掉了。
printf("sizeof(unsigned int): %dn", sizeof(unsigned int));
printf("sizeof(unsigned long): %dn", sizeof(unsigned long));
printf("sizeof(unsigned long long): %dn", sizeof(unsigned long long));
unsigned int uint=0x01020304;
printf("uint: 0x%xn", uint);
unsigned long ulong=0x01020304;
printf("ulong 1: 0x%xn", ulong);
printf("ulong 2: 0x%lxn", ulong);
u... 阅读全帖 |
|
L*****e 发帖数: 8347 | 2 没法直接做数学意义上的转换,不管cast是从singed到unsigned,还是unsigned到
signed,都意味着丢失掉一部分信息,或者说是这部分信息已经转换成别的信息,
在这里就是负号被转成了更大的数。
如果你要signed int和unsigned int混在一起做数学意义的运算的话,可以把负数
当做减去一个正数,然后把这个正数cast成unsigned和别的unsigned的数做运算。。。 |
|
L*****e 发帖数: 8347 | 3 没法直接做数学意义上的转换,不管cast是从singed到unsigned,还是unsigned到
signed,都意味着丢失掉一部分信息,或者说是这部分信息已经转换成别的信息,
在这里就是负号被转成了更大的数。
如果你要signed int和unsigned int混在一起做数学意义的运算的话,可以把负数
当做减去一个正数,然后把这个正数cast成unsigned和别的unsigned的数做运算。。。 |
|
b***y 发帖数: 2799 | 4 ☆─────────────────────────────────────☆
jyu (jyu) 于 (Fri Mar 14 16:55:32 2008) 提到:
hi All,
I have a problem casting a 4 byte unsigned char into integer.
here is my code:
this is C++ code:
// header is just a structure with a field of 4 byte unsigned char
unsigned int sequence = *(reinterpret_cast(header->m_sequenceNum
));
and the sequence is always some overflow #!
all comments/help are appreciated!
Thank you very much!
☆─────────────────────────────────────☆
Xentar (思考猪) 于 |
|
a****l 发帖数: 8211 | 5 unsigned int_8 a; //8bit unsigned integer
signed int_16 b; //16bit signed integer
a=0xFF;
b=a;
What's the value of b? The question is, which happens first, unsigned->
signed or 8bit->16bit? |
|
s****n 发帖数: 786 | 6 This is the rule:
int -> unsigned int -> long -> unsigned long -> long long -> unsigned long
long -> float -> double -> long double |
|
x**e 发帖数: 96 | 7 in C/C++, what is the most portable way to subtract an int from unsigned int?
int a;
unsigned b;
a could be either positive or negative,
b>0 and cannot be represented by int (may overflow)
and I know b>abs(a) and b+abs(a) will not overflow in unsigned int.
is it safe and portable to write
b-=a;
or what is the best way to write it? |
|
t****t 发帖数: 6806 | 8 yes it is ok to write that way. int will be converted to unsigned and the
result is unsigned. negative number can be automatically handled (wrap
around).
but most compiler will issue a warning against this case (if the warning
level is high -- which is recommended). so you better write
b-=(unsigned)a;
int? |
|
L*****e 发帖数: 8347 | 9 convert signed int a to unsigned int时,如果signed是个负数的话,convert后就
是2^32 + a。
convert unsigned int a to signed int时,如果unsigned int比max signed int大的
时候,cast的原则是implementation defined,一般是把最高位convert成正负号,剩
余的31位数convert成int。 |
|
L*****e 发帖数: 8347 | 10 convert signed int a to unsigned int时,如果signed是个负数的话,convert后就
是2^32 + a。
convert unsigned int a to signed int时,如果unsigned int比max signed int大的
时候,cast的原则是implementation defined,一般是把最高位convert成正负号,剩
余的31位数convert成int。 |
|
S*A 发帖数: 7142 | 11 unsigned long long ullong=0x0102030405060708;
这个写法是不太好的,
应该是:
unsigned long long ullong=0x0102030405060708LL;
不然那个长的 constant 有可能被编译器截短。 |
|
g****t 发帖数: 31659 | 12 Unsigned 和 signed的比较应该尽量去掉。
Code应该在尽可能少的假设下成立,才是好code。
Unsigned signed比较,需要假设固定的转换规则,才能成立。这个属于多余的假设。
留着这种code,今后你的CPU, C complier换了怎么办? |
|
g****t 发帖数: 31659 | 13 Unsigned 和 signed的比较应该尽量去掉。
Code应该在尽可能少的假设下成立,才是好code。
Unsigned signed比较,需要假设固定的转换规则,才能成立。这个属于多余的假设。
留着这种code,今后你的CPU, C complier换了怎么办? |
|
g*c 发帖数: 4510 | 14 奇怪为啥这种情况是吧signed convert成unsigned,而不是把unsigned convert成
signed。 |
|
|
|
h**o 发帖数: 548 | 17 一开始我用result = ntohll(n), 但他们说如果n是64bit size, 应该用:
#define my_ntohll(n) ((((unsigned long long)ntohl(n)) << 32) + ntohl(n >>
32))
result = my_ntohll(n).
有人解释以下my_ntohll(n) 是什么意思? |
|
f********f 发帖数: 475 | 18 想将array的每个item从unsigned char变成Hex, 然后concatenate一起变成string.怎
么做呢? 谢谢! |
|
z****e 发帖数: 2024 | 19 unsigned char a[ ]="asfsadfs";
string s(a,a+sizeof a/sizeof *a); |
|
f********f 发帖数: 475 | 20 我的unsigned char Data不是ASCII code, 比如说:
{0x01, 0x11,0x02, 0x00, 0x12, 0x34, 0x56 ....}
我想将其中的0x12, 0x34, 0x56 显示成123456在GUI上. |
|
d****n 发帖数: 1241 | 21 C99 n1256: 7.19.6.1 第9项:
"If any argument is not the correct type for the corresponding conversion
specification, the behavior is undefined"
好奇一下,为什么要用“%x“打印一个unsigned long long呢?一般而言,尽量不要在
C代码里尝试C的这些corner cases。有时候,个人感觉是对的,但是实际上标准里规定
是ub,或者在某些很极端的情况下,标准也是有点含糊(或者有矛盾的)的,例如对于
union的一些操作。。。 |
|
N******K 发帖数: 10202 | 22 int i =-1;
unsigned int j =0
i>=j 输出true
i>=int(j) 才对 |
|
l*********s 发帖数: 5409 | 23 because int is converted to unsigned int before comparsion. |
|
z*y 发帖数: 1311 | 24 and your solution is not correct either
unsigned int j = 2147483648;
int i = 1;
(i > (int)j)
return true |
|
N******K 发帖数: 10202 | 25 这么说来 用 unsigned int 很成问题 |
|
t****t 发帖数: 6806 | 26 有warning你还抱怨什么. 不管是把int转成unsigned还是反过来, 都会有问题的. |
|
N******K 发帖数: 10202 | 27 我把程序里面所有 unsigned int 都换成了int
互相倒腾都会出错 |
|
L*****e 发帖数: 8347 | 28 没听懂,为啥说互相倒腾会出错?还是说你觉得它们之间convert的rule和你认为应该
的不一样?
你觉得int的-1 convert to unsigned int后应该是什么? |
|
k**********g 发帖数: 989 | 29
Firstly, signed and unsigned int have a "common interval".
From zero to INT_MAX (0 .. 2147483647)
When both operands are within this common interval, comparison result will
be correct regardless of each operand's type, and/or the coercion rule.
Therefore, one only needs to check that each operand's value is within this
common interval (prior to coercion). This is how the machine code is
typically generated in languages that have integer range checking.
It is very simple to test this. Just test ... 阅读全帖 |
|
g*****y 发帖数: 7271 | 30 lz可能是希望彻底取缔unsigned吧?话说问题应该也不是很大吧,
如果大家都用64位系统的话。 |
|
c****p 发帖数: 6474 | 31 位运算为主的情况下unsigned还是有用的吧? |
|
N******K 发帖数: 10202 | 32 int i =-1;
unsigned int j =0
i>=j 输出true
i>=int(j) 才对 |
|
l*********s 发帖数: 5409 | 33 because int is converted to unsigned int before comparsion. |
|
z*y 发帖数: 1311 | 34 and your solution is not correct either
unsigned int j = 2147483648;
int i = 1;
(i > (int)j)
return true |
|
N******K 发帖数: 10202 | 35 这么说来 用 unsigned int 很成问题 |
|
t****t 发帖数: 6806 | 36 有warning你还抱怨什么. 不管是把int转成unsigned还是反过来, 都会有问题的. |
|
N******K 发帖数: 10202 | 37 我把程序里面所有 unsigned int 都换成了int
互相倒腾都会出错 |
|
L*****e 发帖数: 8347 | 38 没听懂,为啥说互相倒腾会出错?还是说你觉得它们之间convert的rule和你认为应该
的不一样?
你觉得int的-1 convert to unsigned int后应该是什么? |
|
k**********g 发帖数: 989 | 39
Firstly, signed and unsigned int have a "common interval".
From zero to INT_MAX (0 .. 2147483647)
When both operands are within this common interval, comparison result will
be correct regardless of each operand's type, and/or the coercion rule.
Therefore, one only needs to check that each operand's value is within this
common interval (prior to coercion). This is how the machine code is
typically generated in languages that have integer range checking.
It is very simple to test this. Just test ... 阅读全帖 |
|
g*****y 发帖数: 7271 | 40 lz可能是希望彻底取缔unsigned吧?话说问题应该也不是很大吧,
如果大家都用64位系统的话。 |
|
c****p 发帖数: 6474 | 41 位运算为主的情况下unsigned还是有用的吧? |
|
O*******d 发帖数: 20343 | 42 两幅图像在切换时,我用了颜色混合。 Windows的GDI没有比较好的混合颜色的方法。
我写这个屏保时不打算用OpenGL来做。于是自己写了一个混合颜色的class。 主要想法
是用空间换时间,。颜色混合用查表法。 两个颜色,不管红绿蓝,就是一个2维数组的
index,那个位置的颜色就是事先计算好的混合色。
#include
class ColorBlender
{
public:
ColorBlender(double alpha) { mAlpha = alpha;mLookupTable = NULL;
CreateTable(); }
~ColorBlender() {delete [] mLookupTable ;}
unsigned char Blend(unsigned char firstColor, unsigned char
secondColor);
void Blend(const unsigned char * pFirstColor, const unsi... 阅读全帖 |
|
m********r 发帖数: 334 | 43 1)
#pragma pack(1)
typedef struct
{
unsigned int a:22;
unsigned int b:10;
unsigned int c:20;
unsigned char d:6;
unsigned char e:6;
unsigned int f:10;
unsigned char g:3;
unsigned char h:3;
} S1 ;
#pragma pack()
2)
typedef struct
{
unsigned int a:22;
unsigned int b:10;
unsigned int c:20;
unsigned char d:6;
unsigned char e:6;
unsigned int f:10;
unsigned char g:3;
unsigned char h:3;
} __attribute__ ((packed)) S2 ;
为什么sizeof(S1)=1... 阅读全帖 |
|
s*******g 发帖数: 170 | 44 我的一个解法:
unsigned long calc(unsigned m, unsigned n) {
if(m < 2 || m < 2) return 0;
unsigned long ret = m;
for(unsigned i = 1; i < n; ++i)
ret *= m;
return ret;
}
unsigned findNext2(unsigned val) {
unsigned long ret = INT_MAX;
unsigned smallest = 0;
unsigned sum = 4;
while(smallest <= val) {
unsigned n = 2;
for(unsigned i = n; i <= sum-2; ++i) {
unsigned long result = calc(sum-i, i);
if(i == 2) smallest = result;
... 阅读全帖 |
|
E*****7 发帖数: 128 | 45 Try this one in C++.
(1) Compute N to the power of M;
(2) Insert each of the generated result to std::map as the key of the key-value pair. std::map sorts the key exactly as required by the interview question when each key-value pair is inserted into it. In my code, the "value" is just a placeholder.
#include
#include |
|
n****5 发帖数: 81 | 46 用 C 写了一下,用的递归来处理商和余数。用的unsigned int所以假定输入小于1百万
X1百万
#include
#include
const char* tens[] = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "
Seventy", "Eighty", "Ninety"};
const char* lt20[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "
Sixteen", "Seventeen", "Eighteen", "Nineteen"};
void num2str(unsigned int num)
{
if (num == 0)
return;
el... 阅读全帖 |
|
s***0 发帖数: 117 | 47 class VendingItem
{
public:
typedef boost::shared_ptr Ptr;
enum ItemName
{
CocaCola;
Chips;
Cookies;
Condoms;
//etc
};
const unsigned int mPriceInCents;
const unsigned int getPrice() const {return mPriceInCents;};
}
class Cookies : public VendingItem
{
public:
Cookie(ItemName itemname) :
mPriceInCents(priceInCents){} // get priceInCents from some other
rule.
};
// A Rule is a class that takes 2 inputs
//... 阅读全帖 |
|
s***0 发帖数: 117 | 48 class VendingItem
{
public:
typedef boost::shared_ptr Ptr;
enum ItemName
{
CocaCola;
Chips;
Cookies;
Condoms;
//etc
};
const unsigned int mPriceInCents;
const unsigned int getPrice() const {return mPriceInCents;};
}
class Cookies : public VendingItem
{
public:
Cookie(ItemName itemname) :
mPriceInCents(priceInCents){} // get priceInCents from some other
rule.
};
// A Rule is a class that takes 2 inputs
//... 阅读全帖 |
|
s*******s 发帖数: 1031 | 49 follow一下我的面经。
http://www.mitbbs.com/article_t/JobHunting/32517841.html
整理了我的几个解答的算法,分享一下。欢迎批评指正。
多谢!
1. 写一个程序,找出 5^1234566789893943的从底位开始的1000位数字。
我用的递归+数组大数乘法。
// Caclulate (m^n)%(10^k). Keep the k integer numbers in an array.
// Note: the integer numbers are in reversed in the array
// Assume: m>0, n>0, k>0
// Need to check validity outside of this function.
// call calculate(5, 1234566789893943, 1000) to get result.
// Time complexity: O((log n) * k * k)
// Space complexity: O((log n) * k)
ve... 阅读全帖 |
|
i*****o 发帖数: 105 | 50
This is cool, the code is ugly:
#include
int
has3bits(unsigned c)
{
int n = 0;
while (c) {
n += (c&1);
c >>= 1;
if(n > 3) {
break;
}
}
return n == 3;
}
unsigned
highbit(unsigned c)
{
unsigned h = 0x100;
while (!(h & c) && h) {
h >>= 1;
}
return h;
}
int
c3_next(unsigned *c1, unsigned *c2)
{
unsigned c1_min = 0x100, c2_min;
int found = 0, found1 = 0;
while (!found) {
if (has3bits(*c1... 阅读全帖 |
|