How does a computer know that (int x, y) x << y means shift over y bits? I don’t mean the shift part. I mean the y part. Does the computer shift x by one and subtract one from y until y == 0? If not how does the computer figures out the value of y?
If say y = 10, then the binary representation is 0b1010.
The computer can’t simply take each bit of 1010 and use it, can it?
I am trying to to this for bit sizes larger than 8. Since the values are not simply stored as arrays of standard integers, the container doesn’t represent a value, so overloading the operators << and >> is a bit harder.
However, counting down from a 100 bit number to 0 is somewhat inefficient, so I’m trying to find a way to make the computer understand a bit array faster.
you’re saying you have 2 bit arrays and you’re trying to create a shift operator for them? the easiest way to do it would probably be to convert the right one,
y, into an integer and then shift every bit inxby that amount…..otherwise if you want to operate on it 1 bit at at time it’ll be even less efficient. you’ll look at the first bit, if its 1, shift once, look at the second bit, if its 1, shift twice…i think you have to deal with it as a whole for it to be efficient.assuming
ycan’t fit in an integer…and this is just off the top of my head, but let’s assume anintis only 2 bits, butyis0b1111. then, take the first 2 bits, convert it to an integer (3) and shiftxby that amount. then right-shiftyby 2 (the size of our int), and convert that to an int. this makes 3 again, which you’d have to repeat 4 times (int max + 1) for a total of 12. that, + the 3 we did earlier means we’ve shiftedx15 times which was the value ofy. you can repeat this process for even larger numbers.