Notifications
Clear all

Help with c++ pointers

45 Posts
7 Users
5 Reactions
2,725 Views
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@will Isn't the print result backwards? I would expect to see leftmost as 10001000 and rightmost as 10011001

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2045
Topic starter  

@will 

Read it again, I only changed digit to uint,

Yes I understood that.  I was just adding the point that you can't change the pointer type to indicate that was my belief, it wasn't a correction to you.

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@robotbuilder Ok, I was just reacting to the fact you said you didn't have a C++ compiler while the IDE compiles CPP files all the time. True, that particular piece of code won't compile due to the use of cout but I didn't know that at the time and just wanted to make sure nobody got mislead about the C++ compiler available in the IDE.

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2552
 

Posted by: @zander

@will Isn't the print result backwards? I would expect to see leftmost as 10001000 and rightmost as 10011001

Hey, I just made it work - I don't care what it does 🙂

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2045
Topic starter  

@zander 

I assume the data has to come out in the order it went in.  The actual code is in this thread.

https://forum.dronebotworkshop.com/user-robot-projects/swarm-robotics/paged/4/#post-36043

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@robotbuilder Ah, I failed to understand this is a queue analog not a stack. Now I understand the order at least.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2045
Topic starter  

Again using the online c++ compiler to which you can just copy/paste and run.
https://www.onlinegdb.com/online_c++_compiler

Just playing about with the code. Here message is explicitly a 64 bit register.

 

#include <iostream>

using namespace std;

uint64_t message = 0x8833116622774499;
uint64_t temp = 0; 
int count;
  
uint digit;

int main()
{
    temp = message;

    for (int y = 0;y<64;y++) {
        digit = temp & 0x1;
        temp = (temp >> 1);
        
        cout << digit;
        
        if (count == 7){
            cout << " ";
            count = 0;
        }else{
            count++;
        }
            
    }


    return 0;
}

 

 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@thrandell

Posted by: @thrandell

@robotbuilder

@ronbentley1

@will

For what it's worth, I'm running my builds on a Macbook and the compilier throws this warning:

Silver:build trandell$ make

[  2%] Built target bs2_default

[  4%] Built target bs2_default_padded_checksummed_asm

[  6%] Performing build step for 'ELF2UF2Build'

[100%] Built target elf2uf2

[  7%] No install step for 'ELF2UF2Build'

[  8%] Completed 'ELF2UF2Build'

[ 14%] Built target ELF2UF2Build

Consolidate compiler generated dependencies of target test

[ 15%] Building C object CMakeFiles/test.dir/test.c.obj

/Users/trandell/pico/test/test.c: In function 'main':

/Users/trandell/pico/test/test.c:424:8: warning: assignment to 'uint64_t *' {aka 'long long unsigned int *'} from incompatible pointer type 'uint8_t (*)[8]' {aka 'unsigned char (*)[8]'} [-Wincompatible-pointer-types]

  424 |     ip = &message;            // store address of message array in pointer variable

      |        ^

[ 16%] Linking CXX executable test.elf

[100%] Built target test

Tom

So how is it that you posted the code that you yourself aren't able to compile?

Cheers


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@robotbuilder

Posted by: @robotbuilder

@will 

Changing digit to uint did work.  I don't see why the others did not. I wasn't sure about uint which doesn't indicate its length which probably can be up to 64 bits on a 64 bit bus computer?

There is nothing ISO standard about that, so I would not trust it... there is a reason why C++ adheres to ISO standards!

Cheers


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zander

Posted by: @zander

@will Isn't the print result backwards? I would expect to see leftmost as 10001000 and rightmost as 10011001

Indeed!


   
Ron reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@will

Posted by: @will

Posted by: @zander

@will Isn't the print result backwards? I would expect to see leftmost as 10001000 and rightmost as 10011001

Hey, I just made it work - I don't care what it does 🙂 

If the end result is incorrect, then other than compiling, you made nothing work 😉

Cheers


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zander

Posted by: @zander

@robotbuilder Ah, I failed to understand this is a queue analog not a stack. Now I understand the order at least.

Such data structures (queue vs stack), have nothing to do with this at all... this is all about the true binary representation as far as I can tell?

Cheers


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@robotbuilder

Posted by: @robotbuilder

Again using the online c++ compiler to which you can just copy/paste and run.
https://www.onlinegdb.com/online_c++_compiler

Just playing about with the code. Here message is explicitly a 64 bit register.

#include <iostream>

using namespace std;

uint64_t message = 0x8833116622774499;
uint64_t temp = 0; 
int count;
  
uint digit;

int main()
{
    temp = message;

    for (int y = 0;y<64;y++) {
        digit = temp & 0x1;
        temp = (temp >> 1);
        
        cout << digit;
        
        if (count == 7){
            cout << " ";
            count = 0;
        }else{
            count++;
        }
            
    }


    return 0;
}

 

This code is also backwards, and thus incorrect!

I will post some follow-ups!

Cheers

 

This post was modified 2 years ago by frogandtoad

   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@robotbuilder

Posted by: @frogandtoad

@robotbuilder

Posted by: @robotbuilder

Again using the online c++ compiler to which you can just copy/paste and run.
https://www.onlinegdb.com/online_c++_compiler

Just playing about with the code. Here message is explicitly a 64 bit register.

#include <iostream>

using namespace std;

uint64_t message = 0x8833116622774499;
uint64_t temp = 0; 
int count;
  
uint digit;

int main()
{
    temp = message;

    for (int y = 0;y<64;y++) {
        digit = temp & 0x1;
        temp = (temp >> 1);
        
        cout << digit;
        
        if (count == 7){
            cout << " ";
            count = 0;
        }else{
            count++;
        }
            
    }


    return 0;
}

 

This code is also backwards, and thus incorrect!

I will post some follow-ups!

Cheers

First of all... you can't just shove 64 bits into 8 bits, without special casting techniques, but then again, why would you do such a thing?, I just can't understand why, other than to say the programmer is uneducated!

If you need and want to use 64 bits, then use 64 bits!

Secondly, that is additional code you really don't need.

If you really want to determine the binary representation for a given number, 64 bit or otherwise, then please take note of the following examples:

Arduino 1:

void showBits(uint64_t number) {
  for (int bitPos = 63; bitPos > -1; bitPos--) {
    if (number & ((uint64_t)1 << bitPos))
      Serial.print("1");
    else
      Serial.print("0");
  }
}

void setup()
 {
  Serial.begin(9600);
 }

void loop()
 {
  showBits(0x8833116622774499);

  Serial.println();
  delay(1000);
 }

Note: You need to cast the literal '1' to uint64_t, otherwise it is just interpreted as the smallest integer type by the compiler!

Arduino 2:

void setup()
 {
  Serial.begin(9600);
 }

void loop() 
 {
  uint64_t number = 0x8833116622774499;

  for(int bitPos = 63; bitPos > -1; bitPos--) {
    if (bitRead(number, bitPos))
      Serial.print("1");
    else
      Serial.print("0");
   }
  
  Serial.println();
  delay(1000);
 }

Here we use the built-in Arduino functions to use for bit manipulations.

The Arduino founders built these C++ functions for a reason... they know better than you!

Cheers

This post was modified 2 years ago 2 times by frogandtoad

   
Ron reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 
This post was modified 2 years ago by frogandtoad

   
ReplyQuote
Page 2 / 3