« first day (377 days earlier)      last day (4569 days later) » 

4:00 PM
Are you trying to be funny?
 
@CatPlusPlus What's a TU?
 
@SSight3 Add a second TU.
Translation unit.
 
@CatPlusPlus No idea what that means. Jargon.
 
@codemaker JESUS, what the hell!
 
@LewsTherin you can then resume execution with c, but you probably want to type 'n' (for next), to just step to the next line. Then you can print more stuff
 
4:00 PM
You're claiming to be able to write a better standard library, yet you don't even know C++.
6
 
print buffer
$1 = "\001\000\000\000\000\000\000\000\001\000\000\000\030\371\022\000Z\362\377\277[\362\377\277\001\000\000\000Y\003\033\000[\362\377\277Z\362\377\277\277\000\000\000\001\000\000\000\360WV\000\000\000\000\000x0\264,\364\237\004\b\003\000\000\000)\204\004\b\030\371\022\000%\203\004\b\364\237\004\b\341\207\004\b\000\205\004\b\000\000\000\000\333;\026"
 
@LewsTherin careful. Put on a helmet. That way when your mind is blown, you can find all the peices.
 
@FredOverflow Because it's not caused me compile issues?
 
Jargon is what professionals on a certain field use to communicate with their peers.
 
@codemaker Too late!
 
4:01 PM
Lol.
 
@LewsTherin yup, that is what is in your buffer :). do print strlen(buffer). it will print 1
 
Bug closed: works for me.
 
@RMartinhoFernandes Jargon is another way of saying a person can't explain what they are talking about.
 
@SSight3 So UB is not an issue as well, as long as it doesn't manifest itself? :)
 
@LewsTherin that buffer is 100 chars long, initialized to random data basically. That is the random data that is in it
 
4:01 PM
"But I gave you the code that doesn't work!
 
user784668
@CatPlusPlus it'd not be that hard, really. C++ standard library is quite poor.
 
@SSight3 He gave you a very googleable term.
It's called summarizing.
TemplateArray<int> x; char c = x;
 
— Well I like my test case better."
 
@RMartinhoFernandes Where abouts? I dropped out for a few minutes.
 
@codemaker It shouldn't, I initailized it to 0
 
4:02 PM
@SSight3 "Translation unit". You were not dropped out. You called it "jargon".
 
@Fanael But you cannot write a better standard library if you don't know what the ODR is.
 
@RMartinhoFernandes Oh. Bool mis-usage.
 
Bug in my book.
 
@LewsTherin not in the code you posted to me
 
4:03 PM
Is this the way of Initializing c strings to 0
char c[40]={0,} ?
 
user784668
@FredOverflow: that's a separate issue.
 
@LewsTherin if you type, 'n' then do print buffer it should have different data in it
 
@RMartinhoFernandes I queried about that on SE. The safe bool idiom is nice, but C++11 has the explicit feature, correct?
 
@LewsTherin In C89, you write char c[40] = {0};. In C99 and C++, you can write char c[40] = {};.
 
@SSight3 But you didn't use it.
struct foo : public TemplateArray<T> { void fuck_up() { Clear(); } };
 
4:04 PM
@SSight3 Yes, but no compiler supports it yet, AFAIK.
 
user784668
@RMartinhoFernandes what's that?
 
@Fanael Another bug. Leaks the whole vector.
 
@FredOverflow Oh ok thanks. I will try that. Do the slashes \ represent the next byte? Are the ascii representation of the characters
 
@RMartinhoFernandes No I didn't. Because assigning from an array to int, one should not expect it to work in the first place.
 
@SSight3 But it works in your code.
 
4:05 PM
@LewsTherin \abc is the character with the octal ASCII value abc.
 
@RMartinhoFernandes So does going out of bounds with vector. Doesn't mean it's proper usage.
 
Also, why SetArray takes a pointer?
 
I think I might be too patient.
 
SetArrayButNotReally
 
user784668
@RMartinhoFernandes it's not a leak, it's to prevent at least some of the wild pointers from crashing the program!
 
4:05 PM
@CatPlusPlus So it can copy a template array of the given size.
 
@SSight3 There is a reason the safe bool idiom exists.
 
A template array and not a TemplateArray.
 
this is one of the harshest code reviews I have ever witnessed.
 
@RMartinhoFernandes Yes but the not very nice people of SE didn't really explain how it worked and berated me for asking.
 
He asked for it.
 
4:06 PM
I can link the question if you'd like?
 
@FredOverflow ...
 
@LewsTherin What does "..." mean?
 
@codemaker It's good. I need this harsh review. I take all feedback onboard.
 
@LewsTherin open up a terminal and type "man ascii"
 
@SSight3 Well, here's the link about the safe-bool idiom: artima.com/cppsource/safebool.html
 
4:07 PM
@SSight3 you need more than this harsh review. Hopefully you are learning a lot though.
 
const bool SetArray(const TemplateItem Data[], const SIZE_TYPE S) will cause an access violation when you pass NULL.
 
@codemaker ah sleek
 
Highlights the problems, shows some bad solutions, and presents the best one in the end.
 
@codemaker ASCII man is my favorite superhero!
 
Here's the question I asked ages back:
http://stackoverflow.com/questions/7704395/how-does-the-safe-bool-idiom-bool-type-and-the-safe-bool-idiom-work
 
4:08 PM
@FredOverflow I love it every time he defeats the EVIL UNICODE!
 
Now my buffer has \000
 
@CatPlusPlus It won't. It will return false;
 
0
Q: How does the safe bool idiom bool_type (and the safe bool idiom) work?

SSight3I was pointed to the 'safe bool idiom', and after trying to decipher what is going on (the explanation supplied on the site was not sufficient enough to grant me understanding of why it works), I decided to try to take the following code apart and make an attempt at simplifying it as much as poss...

 
RETURN_BOOL is return false;
 
12
A: Conversion function for error checking considered good?

R. Martinho FernandesIn C++03, you need to use the safe bool idiom to avoid evil things: int x = my_object; // this works In C++11 you can use an explicit conversion: explicit operator bool() const { return is_valid; } This way you need to be explicit about the conversion to bool, so you can no longer do crazy ...

 
4:08 PM
@LewsTherin right, 0. In c 0 is used to mark the end of a string
 
That's how you post a question.
 
And that's for C++11.
 
SetArray(NULL, 42);
You check for S < 0, it passes.
Then you run Copy, which blindly dereferences.
Wham.
 
@CatPlusPlus Yes but then it checks Data == NULL
Or it should do...
 
@codemaker n quit the loop
 
4:09 PM
Right.
It should.
 
Lemme check...
 
@LewsTherin that "man ascii" has a chart with octal, decimal, and hex values for various characters. 32 in decimal is space for example
@LewsTherin eventually, n will quit the loop
@LewsTherin you should still be able to print the buffer though
@LewsTherin n tells the debugger to execute the next line
 
@codemaker Looks useful thanks :)
 
@codemaker That should be \040, right?
 
@codemaker It quits the loop :(
 
user784668
4:10 PM
Structures with no fields are legal, aren't they?
 
@FredOverflow in octal, yeah
 
I'm using strlen it should have continued
 
@Fanael Yes. But they have size at least 1.
There's even an optimization for it.
 
@LewsTherin hmm, don't use strlen() :). strlen() is going to always return 0 if you init the array with 0s
 
@codemaker I never use octal myself, only by mistake when I put a zero in front of a decimal number :)
 
4:11 PM
//IsValid is REQUIRED, notably in the cases of malloc where the internal state is not defined, and in-case of user tampering
This comment is interesting.
 
@FredOverflow i don't use octal either. decimal or hex all the way
 
@LewsTherin strlen's sole job is to find the 0 and then stop.
 
@codemaker Ah, good point! Now I see why it doesn't use that
@FredOverflow yeah lol, I forgot that c101 :(
 
@CatPlusPlus I checked. What happens in the event of NULL + 42?
 
user784668
@RMartinhoFernandes you mean the empty base optimization?
 
user784668
4:12 PM
Or something else?
 
@Fanael Yes.
 
@LewsTherin your loop exits when read returns a value greater than 0. read returns the number of bytes read. So basically when it reads anything, the loop will exit
 
user784668
@RMartinhoFernandes: completely forgot about that, thanks.
 
Can I also have a code review? Here is my code:
int my_strlen(const char * p)
{
    const char * q = p;
    while (*q) ++q;
    return q - p;
}
 
@FredOverflow reviewed
 
4:13 PM
@SSight3 Copy(Temp.Array,Data,S); blows up in that case.
 
@FredOverflow looks like code
 
Boo, strlen, boooo.
 
@codemaker No it shouldn't!
 
@LewsTherin but that's what you have told it to do :)
 
Anything less than 0 should quit the loop
 
4:14 PM
@FredOverflow I demand better variable names.
 
@RMartinhoFernandes Noticed. You are correct. But check with TemplateArray's Remove() function - SetArray should logically have the same fail-safes.
 
while(read>0) if read returns 0 or less it quits...
 
@SSight3 Does knowing that make the bug go away?
 
@LewsTherin doh! you are right. I read that wrong
 
@RMartinhoFernandes Yes. It implies that I should make a unified fail-safe function.
 
4:15 PM
@codemaker that gdb looks useful you are right, it reads everything at once! Wow bloody ashes. I hope I get used to this
I think they locked me in the college
 
@RMartinhoFernandes okay:
int my_strlen(const char * couch_potato)
{
    const char * boyscout = couch_potato;
    while (*boyscout) ++boyscout;
    return boyscout - couch_potato;
}
 
@LewsTherin yeah gdb is useful, intuition is better though
 
FWIW the lesson I'm trying to teach is that you can't ever claim "my code has no bugs".
4
 
user784668
Now, structures with no fields are or are not PODs?
 
@FredOverflow Doesn't help!
 
4:16 PM
@RMartinhoFernandes Hey, I got a free review out of it!
 
@codemaker Another debugger?
 
@Fanael They're PODs
 
@FredOverflow You get the length by taking the end address (boyscout) and deducting couch_potato. Your only issue is in the event of NULL.
 
@LewsTherin no :) intuition. Basically just knowing what the problem is
 
Which is ironic.
 
4:17 PM
@SSight3 Why?
 
@codemaker Oh yeah ha if I understood how it worked I would definitely not need a debugger
 
@FredOverflow Because I made that mistake in three functions.
 
@Fanael They may be non-PODs.
20
Q: What are Aggregates and POD's and how/why are they special?

Armen TsirunyanThis FAQ is about Aggregates and POD's and covers the following material: What are Aggregates? What are POD's (Plain Old Data)? How are they related? How and why are they special? What changes for C++11?

 
Does wireshark work for you @codeoverflow?
 
@SSight3 the normal strlen will fail if you pass it a NULL too, that's kinda the point of his
 
cpx
4:19 PM
hmm anyone saw that?
 
@codemaker Oh. Okay. Well, my code should catch incoming NULLs and blurt them as errors.
 
Yum home made butter cinnamon popcorn
Made properly (not in a microwave)
 
@SSight3 understood, but that's not the point :)
 
You know what? I've used C for a long time, and I have absolutely no idea what strlen is supposed to do in case you pass it NULL.
2
I never needed to know it.
 
@codemaker It works! Had to run it as sudo
 
4:21 PM
@LewsTherin eventually you get to the point where you just know what is going on with the code sometimes
@LewsTherin right, and you can filter on the port your app is using and see the packets
 
And the C standard seems silent on it.
I guess it's just UB.
In that case, Fred's version is not wrong in the face of NULL.
 
@LewsTherin the follow TCP stream option in the analyze menu may be useful for you
 
@codemaker I don't think I can understand that.. Too many numbers
 
@RMartinhoFernandes What happens if I were to call strcpy(NULL,NULL);?
 
@RMartinhoFernandes I don't think my c lib likes strlen(NULL)
@SSight3 disater
 
4:22 PM
> Unless explicitly stated otherwise in the description of a particular function in this subclause, pointer arguments on such a call shall still have valid values, as described in 7.1.4.
 
@KianMayne yes, microwave popcorn is of the devil
 
@RMartinhoFernandes yay me!
 
So, you can't possibly write a strlen that is incorrect when you pass it NULL.
 
@RMartinhoFernandes Theoretically it should return 0. Or... NULL.
 
@RMartinhoFernandes And what are "valid values, as described in 7.1.4"?
 
4:23 PM
@SSight3 No, it has no behaviour defined.
 
@SSight3 strlen does not return a pointer, so it can't return NULL.
 
@RMartinhoFernandes Well, it tells you the string's length right? And a string that is NULL has a length of 0, being non-existent?
 
@LewsTherin it will show you the actual TCP packets going across the network, if you look in the packets you will see bits and pieces of your data
 
@codemaker Indeed - the packet i bought didn't even have hob instructions on it
 
If an argument to a function has an invalid value (such as a value
outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) (...)
 
4:24 PM
@FredOverflow More punning there. A rubbish pun.
 
@SSight3 No.
 
@SSight3 No
 
The string with length zero is this one: "".
 
Why?
 
because it doesn't exist
 
4:25 PM
That has a length of one, one space to place the null.
 
it doesn't have a length of 0, it just doesn't have a length.
 
@SSight3 No, that's the length of the array.
 
@codemaker I am seeing numbers, not packets
 
assert(strlen("") == 0);
 
@RMartinhoFernandes Aha, so it is invalid to pass NULL to strlen?
 
4:25 PM
and I am hungry :( sigh I need food
 
@FredOverflow Yes. That's what I learned today.
 
So how should strlen behave then?
 
@LewsTherin you should see a bunch of stuff
 
and what the C standard says is, "If it's not a real string, then do whatever the fuck you want, because I don't give a hairy, worm-infected shit".
 
@FredOverflow UB.
 
4:25 PM
@RMartinhoFernandes okay then :)
 
@FredOverflow I think it should return 0. Or -1. Or errno.
 
@LewsTherin in my wireshark the raw packet data is at the very bottom when you click on a packet. On the left in hex and on the right in ascii with a '.' representing non printing chars
 
@SSight3 That would introduce unnecessary checking on every call.
 
@SSight3 I was asking about the C standard, not what people think should happen.
 
I think undefined is perfect.
 
4:26 PM
@RMartinhoFernandes That's the length of the string. Null included.
 
passing a NULL string is a bug.
 
Hi, a quick question about C++ and RAII: Say I allocate a byte array on the heap, and then call lots of api functions which use it. If any of them fail, I have to return, always deallocating the array first. Should I use an std::auto_ptr to avoid deleting before every return statement?
 
hence UB is fine
 
@codemaker oh yes, I am seeing lots of bunch of stuff, If you are here later I will talk to you. The college is throwing me out lol thanks a lot man
 
1 min ago, by R. Martinho Fernandes
assert(strlen("") == 0);
Run this and see.
 
4:26 PM
@dario_ramos Use std::vector<byte>
 
@LewsTherin ok, later
 
user784668
@SSight3 what if a valid string had a length of size_t(-1)?
 
@DeadMG: These functions expect an unsigned char*, and they're from a third party library
 
@Fanael Okay, offset by 1. Make 0 error, 1 for "" etc etc.
 
The fact that I used C for years and had no idea what strlen(NULL) was supposed to do should speak ounces.
 
4:27 PM
@dario_ramos That's what &vector[0] is for.
 
There's no reason at all to call strlen(NULL).
 
which is perfectly well-defined behaviour, by the way- vector is guaranteed to be contiguous.
 
Got it
 
@RMartinhoFernandes did you ever try to do it?
 
Thanks!
 
4:28 PM
@SSight3 There is no such thing as null in a string. C strings are terminated by the NUL character (with only a single L) , not the null pointer.
 
no problem
 
user784668
@SSight3 still, what if a valid string had a length of size_t(-1)? strlen would then return 0.
 
@codemaker Nope. I never got curious.
 
@RMartinhoFernandes What if the pointer you pass is accidentally NULL for the sense of the argument?
 
4:28 PM
@SSight3 That's a bug.
 
@FredOverflow Technicality.
@Fanael size_t is unsigned int. You can't have -1.
 
I don't want strlen to be babysitting my code.
 
@DeadMG &vector[0] is awesome
 
@Fanael Arrays of the size "largest size_t value" are forbidden.
 
@RMartinhoFernandes Then isn't passing NULL to SetArray a bug on the developer's part too? Heh heh heh...
 
4:29 PM
@SSight3 Depends on how you define it.
 
user784668
@SSight3: but you can have size_t(-1). And no, size_t is not unsigned int.
 
@RMartinhoFernandes never saw it happen in some code you were debugging?
 
@SSight3 Yes. And it's fine to say "If you pass NULL, then UB."
 
@SSight3 Well, you define your API as you want.
 
user784668
@FredOverflow are they? I didn't know that.
 
4:30 PM
@codemaker I don't remember, but it's possible.
 
@Fanael Pretty sure, yes. Let me see if I can find a source.
 
user784668
@SSight3 it's not.
 
user784668
@SSight3 [citation needed]
 
@Fanael I gave a supporting link. Where's yours?
 
4:31 PM
size_t is an unsigned integer of size suitable to represent the size of arrays.
You can have size_t(-1). It is equal to 2^(sizeof(size_t)*CHAR_BIT)
 
@SSight3 what he is saying is that cplusplus.com is not a source of c++ info
 
@RMartinhoFernandes Which means that every array size can be represented by size_t, not the other way around.
 
@codemaker Gasp! How can this be?
 
@FredOverflow Right, that's what I said.
 
@SSight3 basically cite it from the standard docs
 
4:32 PM
@SSight3 Because it is full of errors.
 
I found!
 
For example, here you can find:
size_t size() const;
 
user784668
@SSight3 from the Visual C++'s crtdefs.h: codepad.org/DFC5Pe50
 
But the standard says the return type is size_type.
 
4:33 PM
In my theoretical implementation, sizeof(size_t) == 100. Deal with it.
 
That sounds terribly inefficient.
 
user784668
This example disproves the claim that size_t is an unsigned int.
 
@FredOverflow That's not one of the design goals.
The main design goal is to be break as many mistaken assumptions as possible, without breaking standard compliance.
 
sounds like an interesting project :)
 
@FredOverflow It's theoretical :(
 
4:35 PM
Please have a NULL pointer whose bit pattern is not all 0.
 
user784668
@RMartinhoFernandes :(
 
user784668
@FredOverflow there are real platforms that have non-0 NULL.
 
@Fanael Actually, that doesn't prove anything. That's a typedef.
 
Yes I know, but other's don't.
 
2 mins ago, by R. Martinho Fernandes
In my theoretical implementation, sizeof(size_t) == 100. Deal with it.
It is a valid implementation.
And sizeof(unsigned int) is not 100.
 
4:36 PM
@Fanael in c++ NULL must = 0 though right?
 
@codemaker No.
When converted to a pointer, 0 must be equal to NULL.
 
http://pubs.opengroup.org/onlinepubs/000095399/basedefs/stddef.h.html
"size_t
Unsigned integer type of the result of the sizeof operator."
 
But NULL doesn't need to be 0.
@SSight3 Right, where does that say it's unsigned int?
 
user784668
@RMartinhoFernandes: does CHAR_BIT equal to 69 in your theoretical implementation?
 
@RMartinhoFernandes is Stroustrup wrong? www2.research.att.com/~bs/bs_faq2.html#null
 
4:37 PM
51 secs ago, by R. Martinho Fernandes
When converted to a pointer, 0 must be equal to NULL.
It's a subtle difference.
@Fanael Haven't decided on that yet.
 
size_t is an unsigned data type defined by several C and C++ standards (e.g., the C99 ISO/IEC 9899 standard) that is defined in stddef.h. It can be further imported by inclusion of stdlib.h as this file internally sub includes stddef.h . This type is used to represent the size of an object or the maximum size of an array. Library functions that take or return sizes expect them to be of this type or have the return type of size_t. Further, the most frequently used compiler-based operator sizeof should evaluate to a value that is compatible with size_t. Range and storage size of size_t Th...
Point is, -1 should not be valid. It's unsigned.
 
> The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int,
@SSight3 unsigned int x = -1; is valid.
 
-1 is still probably a valid unsigned value
 
@codemaker You are confusing the literal 0 with the pointer that results from converting that 0 to the null pointer.
 
It makes x equal to 2^(sizeof(unsigned int)*CHAR_BIT)-1.
 
4:39 PM
You can apply it but without looking
 
I said so before.
 
What number does it contain as a result?
 
31 secs ago, by R. Martinho Fernandes
It makes x equal to 2^(sizeof(unsigned int)*CHAR_BIT)-1.
 
The precise number?
Is that practical that you have to write a function to work it out?
 
@SSight3 Depends on the size of unsigned int.
 
4:40 PM
@FredOverflow ahh ok. Yeah I have never thought of 0 and this "zero pointer" as being different. I have always planned on using nullptr one day so I don't have to care
 
@SSight3 It's the maximum possible value unsigned int can hold.
 
@codemaker sounds like a good plan
 
unsigned int x = ~0;
 
@SSight3 Same thing.
 
-1 would theoretically be undefined.
One is readable.
 
4:41 PM
@SSight3 No, it's well-defined.
 
@SSight3 Are you sure?
 
@SSight3 signed numbers are stored with 2s compliment, so generally -1 is all '1's in binary or 0xfffffffffffffffff in hex which will given you the largest number that can fit in your int type
 
user784668
@RMartinhoFernandes: haha, you're wrong. unsigned int may have padding bits in its internal representation, so 2 ^ (sizeof(unsigned int) * CHAR_BIT) - 1 is not necessarily equal to UINT_MAX.
 
If we take it in a single byte that to represent it, we have 10000001 (signed and the one), and not the 11111111 we'd expect.
 
@Fanael Are you sure of that?
 
4:42 PM
Theoretically, of course.
 
@SSight3 That's one representation.
 
ahh programmers...
 
Minus one can be represented as 11111111 in two's complement.
 
> If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2^n where n is the number of bits used to represent the unsigned type). [Note: In a two's complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). - end note ]
 
And which representation can a compiler use? Either of them?
 
user784668
4:42 PM
@RMartinhoFernandes: kind of. Gonna check the standard.
 
@SSight3 Any one it wants.
@Fanael Fred found it.
 
Then it'd be erroneous to rely on -1 to get a filled byte.
 
@SSight3 Read Fred's quote.
 
@SSight3 right, it only works on a twos-complement machine
 
It's defined to do that.
@FredOverflow It's not erroneous!
 
4:43 PM
So you say it's valid. What kind of valid?
 
uh
 
I can write you a binary statement in assembly
That subtracts
 
On a ones-complement machine, -1 is 11111111111111111111111111111110.
 
@SSight3 The kind of valid that must compile and produce that exact result when ran.
 
Without two's complement.
 
4:44 PM
there's only one kind of valid
 
what about invalid?
 
the valid that says "On my platform and with my compiler, it's guaranteed to work as I expect on all my compile targets."
 
@DeadMG strlen(NULL) is valid. It will compile. It'd also crash.
 
@SSight3 It's not valid.
 
that's not valid
it's just also not caught by the compiler
 
4:44 PM
@DeadMG Because it does not work as expected?
 
what about varying degrees of validity?
 
yep
 
@SSight3 No, because it's said to be invalid by the language.
 
@DeadMG And if -1 does not work as expected?
 
@RMartinhoFernandes I'm not sure what you're saying here. Do you agree or disagree with me? Please elaborate.
 
4:45 PM
@SSight3 The compiler is buggy.
 
it will
 
@RMartinhoFernandes Why is the compiler at fault and not you?
 
I recall the time I learned of the >>> operator in java. Ahh... java
 
x86 is defined to be two's complement, so as long as I always target x86, then it will always work as expected
 
> Note: In a two's complement representation, this conversion is conceptual and there is no change in the bit pattern
So -1 is not guaranteed to be all 1s when converted to an unsigned int. It only works on a twos-complement machine.
 
4:45 PM
@SSight3 Because the language defines -1 to work as I said.
 
most, if not all, major architectures also work this way
 
@RMartinhoFernandes Where?
 
@FredOverflow Oh, if you talk about bit patterns, you're right. Iwas thinking of UINT_MAX.
@SSight3 Fred quoted it.
Twice.
For Knuth's sake people read the fucking messages.
4 mins ago, by FredOverflow
> If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2^n where n is the number of bits used to represent the unsigned type). [Note: In a two's complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). - end note ]
 
1 min ago, by FredOverflow
So -1 is not guaranteed to be all 1s when converted to an unsigned int. It only works on a twos-complement machine.
 
(Yes, I know the math terms may be weird.)
 
4:47 PM
He also said it would not work on 1's complement machines.
 
3 mins ago, by FredOverflow
On a ones-complement machine, -1 is 11111111111111111111111111111110.
 
So ~0 I think.
That looks so wrong.
 
@SSight3 Right. ~0 is a portable way to get all ones, whereas -1 is not.
 
@FredOverflow On a one's complement machine unsigned int x = -1 produces all ones.
 
@FredOverflow Thank you for the demonstration. Well
 
4:48 PM
It's required to.
 
facepalm I didn't even think of doing ~0. But that won't always give you UINT_MAX will it?
 
I know a method that is neither one's complement nor twos
 
It's not as efficient as in a two's complement machine, though.
 
That works for binary subtraction
 
@RMartinhoFernandes No it will not. Have you ever worked with ones-complement?
 
4:48 PM
@FredOverflow Read your quote.
 
@codemaker It will. Well, unless it's signed, then max signed. But you can toggle the bits.
 
@RMartinhoFernandes Oh fuck me, you are right!
 
-1 when converted to an unsigned type must be 2^N-1, where N is the number of bits.
 
I was getting lost in the note and didn't pay attention to the main text. This was interesting.
The note simply says that this is particularly efficient on a twos-complement machine.
 
4:50 PM
stupid me lol
 
The standard does not require it, but it makes it the most natural implementation.
 
Having the choice between being right and learning something new, I always prefer the latter.
 
Okay
In what instance does 2's complement overload?
 
You mean overflow?
 
4:54 PM
15 - 1 or 1 - 15?
 
Overflows are only defined for unsigned types.
 
Overflow, you get what I mean.
No, there's an issue where two's complement can overflow when performing the subtraction/addition.
Signed or unsigned
 
That's easy.
INT_MAX + 1, which is undefined, or UINT_MAX+1 which is zero.
 
No no
Two's complement is the subtraction method
You could put in two variables, either 1 - 15 or 15 - 1
And it'd break
(assuming you had a nibble)
 
INT_MIN - 1?
 
4:56 PM
That's C++ which is high level abstraction.
 
Lemme check online
 
(You can't represent 15 with two's complement on 4-bits: the minimum is -8 and the maximum is 7.)
 
ok, I"m trying to rotate this tetromino made up for four individual squares by a certain angle (in degrees). I've found the formulas to apply to coordinates, however my code doesn't produce the right result. Can anyone provide some assistance
I want to turn it by turning each individual square it is made up of, but I'm not sure that is the correct way
 
@TonyTheLion Wait, you have angles other than 90º, and -90º?
 

« first day (377 days earlier)      last day (4569 days later) »