So I am trying to implement this algorithm in C for multiplying 32-bit unsigned int in order to understand it better:
Step 1: Test multiplier-0
Step 2: if 1, add multiplicand to left half of product
and place the result in the left half of
the product register
Step 3: shift multiplier right 1 bit
Step 4: shift product register right 1 bit
What I am not getting is how to implement step 2. It says to add multiplicand to left half of product and store in left half of product register. I am confused on how to add only to the left half of the product. How do I go about this?
EDIT:
This is something I came with but it does not give me the right answer and I am not sure what is going wrong. Please help!
long unsigned UnsignedMult(unsigned multiplicand, unsigned multiplier){
unsigned int temp32a, temp32b;
unsigned long temp64;
unsigned long product;
int i;
product = multiplier;
temp32b = multiplicand;
for(i=0; i < 32; i++){
if((product & 1)==1){ //add
temp64 = product;
temp64 = temp64 >> 32;
temp32a = temp64;
product = BinaryAdd(temp32a, temp32b);
}
product = product >>= 1;
}
return product;
}
int BinaryAdd(int in1, int in2){
int sum, carry;
sum = in1 ^ in2; // x XOR y
carry = in1 & in2; // x AND y carry in
int i;
for (i = 0; i < 32; i++) {
carry = carry << 1;
in1 = sum;
in2 = carry;
sum = in1 ^ in2; //calculate sum
carry = in1 & in2; //find carry out
}
return sum;
}
Your product register needs to be 64 bits in length to allow two 32 bit integers to be multiplied. Hopefully you have uint64_t available in your compiler to represent this (stdint.h).
To do the addition, you could put your multiplicand into a 64 bit integer, shift it left 32 bits, and then add it to the 64 bit product register.
Something like:
uint64_t tmpMulti;
uint64_t productRegister = 0;
uint32_t multiplicand = 123;
tmpMulti = multiplicand;
tmpMulti <<= 32;
productRegister += tmpMulti;
(Apologies for any syntax mistakes, I haven't written C code in a long time)
Out of interest, I had a go at implementing it myself. This seems to work:
#include <stdio.h>
#include <stdint.h>
void main(int argc, char* argv[])
{
uint32_t multiplier = 17;
uint32_t multiplicand = 12;
uint64_t productRegister = multiplier;
for (int n = 0; n < 32; n++) {
if (productRegister & 1 == 1) {
productRegister += ((uint64_t)multiplicand) << 32;
}
productRegister >>= 1;
}
printf("Result: %d\n", productRegister);
}
The following code does not use <stdint.h>, and uses two 32 bit ints to represent the 64 bit product register. It doesn't attempt to handle overflow, and assumes the answer will fit in 32 bits.
#include <stdio.h>
void main(int argc, char* argv[])
{
unsigned int multiplier = 17;
unsigned int multiplicand = 12;
unsigned int productRegisterLower = multiplier;
unsigned int productRegisterUpper = 0;
for (int n = 0; n < 32; n++) {
if (productRegisterLower & 1 == 1) {
productRegisterUpper += multiplicand;
}
productRegisterLower >>= 1;
productRegisterLower |= productRegisterUpper << 31;
productRegisterUpper >>= 1;
}
printf("Result: %d\n", productRegisterLower);
}
In order to handle the right shift of the product register, it moves the least significant bit of the upper half into the most significant bit of the lower half. To do this, it:
Shifts the lower half to the right 1 bit.
Takes a copy of the upper half and shifts it left 31 bits, so that the least significant bit is now on the left, and the rest of the value is zero.
ORs this with the lower half, to copy the shifted bit across.
Shifts the upper half to the right 1 bit.
Related
I'm unsure if this is possible due to structure padding and alignment but, assuming you take care of that by aligning your structures to 4/8 bytes, is it possible to bit shift on a structure as if it was a single variable?
What I'd like to do is take a string (max 8 bytes) and shift it into the high order bits of a 64-bit variable.
Like if I do this:
#include <stdint.h>
#include <string.h>
void shiftstr(uint64_t* t,char* c,size_t len){
memcpy(t, c, len);
//now *t==0x000000617369616b
*t<<=(sizeof(uint64_t)-len)*8;
//now *t==0x617369616b000000
}
int main(){
uint64_t k = 0;
char n[] = "kaisa";
shiftstr(&k, n,strlen(n));
return 0;
}
This works just fine, but what if I had, instead of a uint64_t, two uint32_t, either as individual variables or a structure.
#include <stdint.h>
#include <string.h>
struct U64{
uint32_t x;
uint32_t y;
};
void shiftstrstruct(struct U64* t, char* c, size_t len){
memcpy(t, c, len);
/*
At this point I think
x == 0x7369616b
y == 0x00000061
But I could be wrong
*/
//but how can I perform the bit shift?
//Where
//x==0x0000006b
//y==0x61697361
}
int main(){
char n[] = "kaisa";
struct U64 m = {0};
shiftstrstruct(&m, n, strlen(n));
return 0;
}
Up to the memcpy part, it should be the same as if I were performing it on a single variable. I believe the values of x and y are correct in such situations. But, if that's the case that means the values need to be shifted away from x towards y.
I know I can cast but what if I wanted to deal with a 16 byte string that needed to be shifted into two 64 bit variables, or even larger?
Is shifting structures like this possible? Is there a better alternative?
Is shifting structures like this possible?
No, not really. Even if the x and y members are in adjacent memory locations, bit-shift operations on either are performed as integer operations on the individual variables. So, you can't shift bits "out of" one and "into" the other: bits that "fall off" during the shift will be lost.
Is there a better alternative?
You would have to implement such a multi-component bit-shift yourself – making copies of the bits that would otherwise be lost and somehow masking those back into the result, after shifting other bits internally to each 'component' variable. Exactly how to do this would largely depend on the use case.
Here's one possible implementation of a right-shift function for a structure comprising two uint64_t members (I have not added any error-checking for the count, and I assume that uint64_t is exactly 64 bits wide):
#include <stdio.h>
#include <stdint.h>
typedef struct {
uint64_t hi;
uint64_t lo;
} ui128;
void Rshift(ui128* data, int count)
{
uint64_t mask = (1uLL << count) - 1; // Set low "count" bits to 1
uint64_t save = data->hi & mask; // Save bits that fall off hi
data->hi >>= count; // Shift the hi component
data->lo >>= count; // Shift the lo component
data->lo |= save << (64 - count); // Mask in the bits from hi
return;
}
int main()
{
ui128 test = { 0xF001F002F003F004, 0xF005F006F007F008 };
printf("%016llx%016llx\n", test.hi, test.lo);
Rshift(&test, 16);
printf("%016llx%016llx\n", test.hi, test.lo);
return 0;
}
A similar logic could be used for a left-shift function, but you would then need to save the relevant upper (most significant) bits from the lo member and mask them into the shifted hi value:
void Lshift(ui128* data, int count)
{
uint64_t mask = ((1uLL << count) - 1) << (64 - count);
uint64_t save = data->lo & mask;
data->hi <<= count;
data->lo <<= count;
data->hi |= save >> (64 - count);
return;
}
union is your friend, this is what you want:
#include <stdint.h>
#include <stdio.h>
typedef union _shift_u64{
struct _u64{
uint32_t x;
uint32_t y;
} __attribute__((__packed__)) U64;
uint64_t x_and_y;
} SHIFT_U64;
int main(int argc, char* argv[]){
SHIFT_U64 test;
test.U64.x = 4;
test.U64.y = 8;
printf("test.U64.x=%d, test.U64.y=%d, test.x_and_y=%ld\n", test.U64.x, test.U64.y, test.x_and_y);
test.x_and_y<<=1;
printf("test.U64.x=%d, test.U64.y=%d, test.x_and_y=%ld\n", test.U64.x, test.U64.y, test.x_and_y);
test.x_and_y>>=1;
printf("test.U64.x=%d, test.U64.y=%d, test.x_and_y=%ld\n", test.U64.x, test.U64.y, test.x_and_y);
return 0;
}
EDIT: This simple program illustrates how to do it the other way, but you have to check for the carry over bit and shift overflow and shift underflow by yourself. union doesn't care about the data, you just have to make sure that the data makes sense. After compiling, redirect the output of the program to a file or hex-editor and read the errorlevel of the program.
Linux example: ./a.out > a.out.bin; echo "errorlevel=$?"; xxd a.out.bin
#include <stdio.h>
typedef union _shift_it{
struct _data{
unsigned long x : 64;
unsigned long y : 64;
} __attribute__((__packed__)) DATA;
unsigned char x_and_y[16];
} __attribute__((__packed__)) SHIFT_IT;
int main(int argc, char* argv[]){
SHIFT_IT test;
int errorlevel = 0;
//bitmask for shift operation
static const unsigned long LEFT_SHIFTMASK64 = 0x8000000000000000;
static const unsigned long RIGHT_SHIFTMASK64 = 0x0000000000000001;
//test data
test.DATA.x = 0x2468246824682468; //high bits
test.DATA.y = 0x1357135713571357; //low bits
//binary output to stdout
for(int i=0; i<16; i++) putchar(test.x_and_y[i]);
//left shift
if(test.DATA.x & LEFT_SHIFTMASK64) errorlevel += 1;
test.DATA.x <<= 1;
if(test.DATA.y & LEFT_SHIFTMASK64) errorlevel += 2;
test.DATA.y <<= 1;
//binary output to stdout
for(int i=0; i<16; i++) putchar(test.x_and_y[i]);
//right shift
if(test.DATA.y & RIGHT_SHIFTMASK64) errorlevel += 4;
test.DATA.y >>= 1;
if(test.DATA.x & RIGHT_SHIFTMASK64) errorlevel += 8;
test.DATA.x >>= 1;
//binary output to stdout
for(int i=0; i<16; i++) putchar(test.x_and_y[i]);
//right shift
if(test.DATA.y & RIGHT_SHIFTMASK64) errorlevel += 16;
test.DATA.y >>= 1;
if(test.DATA.x & RIGHT_SHIFTMASK64) errorlevel += 32;
test.DATA.x >>= 1;
//binary output to stdout
for(int i=0; i<16; i++) putchar(test.x_and_y[i]);
//left shift
if(test.DATA.x & LEFT_SHIFTMASK64) errorlevel += 64;
test.DATA.x <<= 1;
if(test.DATA.y & LEFT_SHIFTMASK64) errorlevel += 128;
test.DATA.y <<= 1;
//binary output to stdout
for(int i=0; i<16; i++) putchar(test.x_and_y[i]);
return errorlevel;
}
I'm wondering if someone know effective approach to calculate bits in specified position along array?
Assuming that OP wants to count active bits
size_t countbits(uint8_t *array, int pos, size_t size)
{
uint8_t mask = 1 << pos;
uint32_t result = 0;
while(size--)
{
result += *array++ & mask;
}
return result >> pos;
}
You can just loop the array values and test for the bits with a bitwise and operator, like so:
int arr[] = {1,2,3,4,5};
// 1 - 001
// 2 - 010
// 3 - 011
// 4 - 100
// 5 - 101
int i, bitcount = 0;
for (i = 0; i < 5; ++i){
if (arr[i] & (1 << 2)){ //testing and counting the 3rd bit
bitcount++;
}
}
printf("%d", bitcount); //2
Note that i opted for 1 << 2 which tests for the 3rd bit from the right or the third least significant bit just to be easier to show. Now bitCount would now hold 2 which are the number of 3rd bits set to 1.
Take a look at the result in Ideone
In your case you would need to check for the 5th bit which can be represented as:
1 << 4
0x10000
16
And the 8th bit:
1 << 7
0x10000000
256
So adjusting this to your bits would give you:
int i, bitcount8 = 0, bitcount5 = 0;
for (i = 0; i < your_array_size_here; ++i){
if (arr[i] & 0x10000000){
bitcount8++;
}
if (arr[i] & 0x10000){
bitcount5++;
}
}
If you need to count many of them, then this solution isn't great and you'd be better off creating an array of bit counts, and calculating them with another for loop:
int i, j, bitcounts[8] = {0};
for (i = 0; i < your_array_size_here; ++i){
for (j = 0; j < 8; ++j){
//j will be catching each bit with the increasing shift lefts
if (arr[i] & (1 << j)){
bitcounts[j]++;
}
}
}
And in this case you would access the bit counts by their index:
printf("%d", bitcounts[2]); //2
Check this solution in Ideone as well
Let the bit position difference (e.g. 7 - 4 in this case) be diff.
If 2diff > n, then code can add both bits at the same time.
void count(const uint8_t *Array, size_t n, int *bit7sum, int *bit4sum) {
unsigned sum = 0;
unsigned mask = 0x90;
while (n > 0) {
n--;
sum += Array[n] & mask;
}
*bit7sum = sum >> 7;
*bit4sum = (sum >> 4) & 0x07;
}
If the processor has a fast multiply and n is still not too large, like n < pow(2,14) in this case. (Or n < pow(2,8) in the general case)
void count2(const uint8_t *Array, size_t n, int *bit7sum, int *bit4sum) {
// assume 32 bit or wider unsigned
unsigned sum = 0;
unsigned mask1 = 0x90;
unsigned m = 1 + (1u << 11); // to move bit 7 to the bit 18 place
unsigned mask2 = (1u << 18) | (1u << 4);
while (n > 0) {
n--;
sum += ((Array[n] & mask1)*m) & mask2;
}
*bit7sum = sum >> 18;
*bit4sum = ((1u << 18) - 1) & sum) >> 4);
}
Algorithm: code is using a mask, multiply, mask to separate the 2 bits. The lower bit remains in it low position while the upper bit is shifted to the upper bits. Then a parallel add occurs.
The loop avoids any branching aside from the loop itself. This can make for fast code. YMMV.
With even larger n, break it down into multiple calls to count2()
int main()
{
double hexa_number;
double bitmask = 0x80;
double i;
printf("Enter 8 bit number in hexadecimal form: ");
scanf("%lf",& hexa_number);
for( i = 0; i <= 8; i++)
{
if(hexa_number&(bitmask >> i))
printf("1");
else
printf("0");
}
return 0;
}
plus Displaying the binary representation of this number,
along with a count of the number of 0’s and 1’s in the binary number.
i found other ways to convert it but not with bitmask
The problem could be that you are using a double for your bitmask. I don't think >> makes much sense for doubles especially as they are composed of a sign bit, mantissa and 2^ exponent.
For example, 8 >> 1 is 4. That is a nice consistent operation, same as division by 2.
If we imagine an unsigned two byte float for simplicity and imagine that the first byte represents the mantissa and the second the exponent (both unsigned), then 7 * 2^4 (=112) could be represented as 0b00000111 00000100. If you shift that 1 to the right, >> 1, you will get 0b00000011 10000010, which by our convention is now 3 * 2^130. This is definitely not the division by 2 you would get by shifting an integer right by 1.
My compiler gives "error: invalid operands to binary >> (have double and unsigned)" if I try bitwise shifting a floating point number.
So basically you could try the following:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int hexa_number = 0;
unsigned num_bits = sizeof(int)*8;
unsigned bitmask = 1 << (num_bits - 1);
printf("Enter a number in hexadecimal form: ");
scanf("%i", &hexa_number);
unsigned k = 0;
unsigned num_ones = 0;
unsigned num_zeros = 0;
for(k = 0; k != num_bits; k ++)
{
if(hexa_number & (bitmask >> k))
{
printf("1");
num_ones ++;
}
else
{
printf("0");
num_zeros++;
}
}
printf("\nNumber of ones is %i\nNumber of zeros is %i", num_ones, num_zeros);
return 0;
}
I am writing a program that implements Booth's Algorithm to multiply ints. I am only allowed to use bit-level operators, logical operators, and bit shifts. I am allowed one loop for the program, and a function that will add two ints.
I am having trouble understanding what is going wrong with my program. When I am using bitwise operators to mask half of the long long, I am getting incorrect values. Any advice on what I am doing wrong there? Maybe I am not understanding how to mask properly. It also may have to do with type casting; I am really unsure. Thanks in advance for any help!
Here is by code:
#include <stdio.h>
int Add (int x, int y);
long long Booth (int x, int y);
int main() {
int hex1, hex2;
long long product;
printf("\nEnter Multiplicand & Multiplier in hex: ");
scanf(" %x %x", &hex1, &hex2);
product = Booth(hex1, hex2);
printf("Multiplicand = 0x%08X \tAs signed = %+d\n", hex1, hex1);
printf("Multiplier = 0x%08X \tAs signed = %+d\n", hex2, hex2);
printf("Product = 0x%16X \tAs signed = %+d\n", product, product);
}
int Add (int x, int y) {
return x + y;
}
long long Booth (int multiplicand, int multiplier) {
int i;
long long product;
long long productLH;
long long productRH;
int productLHI;
int productRHI;
int cOut;
int negMultiplicand;
negMultiplicand = Add (~multiplicand, 1);
product = (long long) Add (0, multiplier);
for (i = 0; i < 32; i++) {
if (((product & 1) == 1) && (cOut == 0)) {
//Mask left half and right half of product
productLH = (product & 0xFFFFFFFF00000000);
productRH = (product & 0x00000000FFFFFFFF);
//Bit shift product so all values are in lower 32 bits
productLH = (productLH >> 32);
//Convert left halves and right halves to ints
productLHI = (int) (productLH & 0x00000000FFFFFFFF);
productRHI = (int) productRH & (0x00000000FFFFFFFF);
productLHI = Add(productLHI, negMultiplicand);
//Put halves back together
product = (long long) Add(productLHI, 0);
product = ((product << 32) & 0xFFFFFFFFFFFFFFFF);
product = (long long) Add((int)product, productRHI);
}
else if (((product & 1) == 0) && (cOut == 1)) {
//Mask left half and right half of product
productLH = (product & 0xFFFFFFFF00000000);
productRH = (product & 0x00000000FFFFFFFF);
//Bit shift product so all values are in lower 32 bits
productLH = (productLH >> 32);
//Convert left halves and right halves to ints
productLHI = (int) (productLH & 0x00000000FFFFFFFF);
productRHI = (int) productRH & (0x00000000FFFFFFFF);
productLHI = Add(productLHI, multiplicand);
//Put halves back together
product = (long long) Add(productLHI, 0);
product = ((product << 32) & 0xFFFFFFFFFFFFFFFF);
product = (long long) Add((int)product, productRHI);
}
cOut = (product & 1);
product = product >> 1;
}
return product;
}
Return of function Add() may not be integer type but you are returning int type. If we add two integers may result more than int type.
You don't need those masks to solve the problem. If the masking is the problem, just bitwise or the product and the multiplier, which will set the lower 32 bits of the 64 bit product to the "right half" you want. Then, just pass the product shifted 32 bits to the right, cast to an int in your addition function call. This will truncate the 64 bit binary to 32 bit binary.
PS. Starting Babic's lab assignments the night before ensures you're going to be in for a bad time...
In addition to the errors rslemos noticed, there are still two things to correct:
The integer constants 0xFFFFFFFF00000000 and 0xFFFFFFFFFFFFFFFF need a suffix with LL.
When adding something to product, the value added must not sign-extend. Thus, change
product = (long long) Add (0, multiplier);
to
product = (unsigned)multiplier; // do not sign-extend multiplier
and
product = (long long) Add((int)product, productRHI);
to
product |= (unsigned)productRHI; // do not sign-extend productRHI
(two occurrences).
I'm rather ashamed to admit that I don't know as much about bits and bit manipulation as I probably should. I tried to fix that this weekend by writing some 'reverse the order of bits' and 'count the ON bits' functions. I took an example from here but when I implemented it as below, I found I had to be looping while < 29. If I loop while < 32 (as in the example) Then when I try to print the integer (using a printBits function i've written) I seem to be missing the first 3 bits. This makes no sense to me, can someone help me out?
Thanks for everyone's help, I've added comments to show changes I've made.
int reverse(int n)
{
int r = 0;
int i = 0;
for(i = 0; i < 29; i++) //Should be i < 32
{
r = (r << 1) + (n & 1); //| instead of + to make it obvious I'm handling bits
n >>=1;
}
return r;
}
Here is my printBits function:
void printBits(int n)
{
int mask = 0X10000000; //unsigned int mask = 0X80000000;
while (mask)
{
if (mask & n)
{
printf("1");
}
else
{
printf("0");
}
mask >>= 1;
}
printf("\n");
}
And a working? reverse function
int reverse2(int n)
{
int r = n;
int s = sizeof(n) * 7; // int s = (sizeof(n) * 8) -1
for (n >>= 1; n; n >>=1)
{
r <<=1;
r |= n & 1;
s--;
r <<= s;
return r;
}
int mask = 0X10000000;
puts a 1 in bit 28. You want 0X80000000.
You have:
int mask = 0x10000000;
There are two problems here. You don't have the high bit set, and if you did, it still (probably) wouldn't work, as your compiler would be using arithmetic shift on a signed int.
You want to change your mask to:
unsigned int mask = 0x80000000;
For arithmetic shift, shifting 0x80000000 right will never become zero, as the sign bit will be magically extended into the other bits. See here for more details on arithmetic shift.
Print Bits is wrong, its 0x80000000 not 0x10000000.
>>> bin (0x80000000)
'0b10000000000000000000000000000000'
>>> bin (0x10000000)
'0b10000000000000000000000000000'
See 0x1... doesnt set the highest bit.
Instead of +, you should use | (bitwise or). And you should use < 32.
As written, this will reverse the lower 29 bits of n into r. The top three bits of n will be left in n (shifted down 29 bits) and not returned.
I would suspect a problem with your printBits function if you see something else.
edit
Your printBits function prints the lower 29 bits of n, so it all makes sense.