![]() |
|
Tutorial Bit masking and shifts - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: Tutorial Bit masking and shifts (/Thread-Tutorial-Bit-masking-and-shifts) |
RE: Bit masking and shifts - phyrrus9 - 08-31-2015 (08-31-2015, 01:51 AM)eclipse Wrote: What's the point of having a carry? Surely 11000000 -> 10000001 --> 00000011 is more useful than 11000000 --> 10000000 --> 00000001 --> 00000011 its useful for data conversion. Take for example the operation 0xff+0x1. We know that the maximum value a byte can hold is 0xff, but yet somehow it knows that 0xff+0x1=0x100. This is done with the carry. Rotating through carry, not always useful, but shifting through it is. 0x8f * 0x2. We do that by shifting it left once through the carry. Then we know that there was an overflow (or can handle multibyte oprations) since most CPU's do not set the V flag on unsigned operations. RE: Bit masking and shifts - 0xDEAD10CC - 08-31-2015 Though there's no real point in doing this: Code: else if (opts & OURMALLOC_SMAX) //mask off bit #3
securemax = 1;And having a second if statement structure which checks the values after the fact for each individual variable set in the first if statement structure. It's also still easy to create a bit rotation macro without the ASM: Code: #define rotr(x) ((((x) & 1) << 7) | (((x) & 0xFE) >> 1))
#define rotl(x) (((x) & 0x80) >> 7) | (((x) & 0x7F) << 1)RE: Bit masking and shifts - phyrrus9 - 09-01-2015 (08-31-2015, 07:18 AM)0xDEAD10CC Wrote: It's also still easy to create a bit rotation macro without the ASM: Yes, but that is bit rotation without carry. C exposes no access to the carry bit without use of assembly, therefore it is not possible to create a true rotate through carry or shift through carry. It amuses me how you vanish until I make a post in the coding forum, and you have to attempt to 1-up me. RE: Bit masking and shifts - 0xDEAD10CC - 09-02-2015 (09-01-2015, 01:22 AM)phyrrus9 Wrote: Yes, but that is bit rotation without carry. C exposes no access to the carry bit without use of assembly, therefore it is not possible to create a true rotate through carry or shift through carry. If you have forgotten, maybe you should take a look at the last posted thread in this section to confirm that there are no others that are recent, and none that I haven't already viewed and likely posted in since my last visit. Your implied logic about me making a post whenever you make a thread is clearly flawed for that reason. Additionally, it's still a rotation. Who cares about the carry bit unless you are interested in using assembly or checking for other arithmetic problems? Furthermore, I never said it was bit rotation with using the carry bit, as I was directly posting in response to this: Quote:Bit rotations without carry Which talks about bit rotation WITHOUT the carry. Although there is no C operator for it, you can still do it as I've shown. Not sure why you're arguing with me now about that, and how I don't use the carry bit because it's irrelevant in this case anyways... The other thing you can do in C for option bitflags is to use bitfields. Code: typedef struct option_flags
{
union
{
struct
{
int option1 : 1;
int option2 : 1;
int option3 : 1;
int option4 : 1;
int option5 : 1;
int option6 : 1;
int option7 : 1;
int option8 : 1;
} opt;
int value;
} u;
} option_flags;
int main(void)
{
option_flags o;
memset(&o, 0, sizeof(option_flags));
o.u.opt.option1 ^= 1;
o.u.opt.option8 ^= 1;
printf("value: %d\n", o.u.value);
return 0;
}RE: Bit masking and shifts - dotcppfile - 09-02-2015 (09-02-2015, 03:50 PM)0xDEAD10CC Wrote: If you have forgotten, maybe you should take a look at the last posted thread in this section to confirm that there are no others that are recent, and none that I haven't already viewed and likely posted in since my last visit. Your implied logic about me making a post whenever you make a thread is clearly flawed for that reason. And you can use the popular set of macros for c++(getmask, field, etc..) which make things even easier; there's probably a million way to do this... RE: Bit masking and shifts - 0xDEAD10CC - 09-02-2015 (09-02-2015, 04:38 PM)dotcppfile Wrote: And you can use the popular set of macros for c++(getmask, field, etc..) which make things even easier; there's probably a million way to do this... True, and bitfields are available to C++ as well, but this thread seemed to be geared specifically to C by what was written in the first post. i.e. Quote:One of the hardest concepts to grasp with C (or most GPPLs) is the concepts of masking and shifting (and likewise rotating with and without carry). RE: Bit masking and shifts - phyrrus9 - 09-03-2015 Can we stay on topic here? This isn't a debate about semantics, its a thread explaining the concepts of masking and bit shifts in C. either stay on the topic or stop fucking loading every thread with spam. @Eclipse this needs a /thread RE: Bit masking and shifts - 0xDEAD10CC - 09-03-2015 (09-03-2015, 12:50 AM)phyrrus9 Wrote: Can we stay on topic here? This isn't a debate about semantics, its a thread explaining the concepts of masking and bit shifts in C. either stay on the topic or stop fucking loading every thread with spam. @Eclipse this needs a /thread Are you retarded? We are still on topic if you know what dotcppfile's macro suggestions are related to. You are the only one spamming up this thread because you seem to find a way to disapprove of any relevant conversation in your threads and find some flawed logic to describe it as off topic. Let this be a reminder -> Topic Title: "Bit masking and shifts" dotcppfile: "And you can use the popular set of macros for c++(getmask, field, etc..) " And using bitfields you can easily mask off single bits, just without having to know any binary arithmetic. Use your small brain for once. I posted this inline asm in the wrong thread, so I'll post it here anyways: Code: __declspec (naked) int rcr(int value)
{
__asm
{
push ebp
mov ebp, esp
mov eax, dword ptr[ebp + 8]
rcr eax, 1
mov esp, ebp
pop ebp
ret
}
}I could have also used 'leave' but decided to be more verbose with the function epilogue. There's no C operator for sqare root either, but yet programmers still find a way to deal with that, and without having to move over to ASM instructions to do so. My previous macros are easily comparable to that concept. RE: Bit masking and shifts - dotcppfile - 09-03-2015 (09-03-2015, 12:50 AM)phyrrus9 Wrote: Can we stay on topic here? This isn't a debate about semantics, its a thread explaining the concepts of masking and bit shifts in C. either stay on the topic or stop fucking loading every thread with spam. @Eclipse this needs a /thread We are on topic mate? I dont get why you're so mad about us having a discussion? Calm down mate... RE: Bit masking and shifts - 0xDEAD10CC - 09-04-2015 The other trick I use bitshifts for is to efficiently round down to a certain value. Most compilers will use similar tricks in the background if you don't keep your stack properly aligned to optimize for performance between your local variable allocation's through manipulations to the stack pointer directly and any push instructions... But here's an example: Code: #include <stdio.h>
int main(void)
{
int x = 42;
x &= 0xFFFFFFF0;
printf("%d\n", x);
return 0;
}This will take away from x the proper value to round the value down to the nearest multiple of 16, in this case 32. |